LayerManagement: fixed issues detected by valgrind
[profile/ivi/layer-management.git] / LayerManagerPlugins / Communicators / GenericCommunicator / src / GenericCommunicator.cpp
index 7ca35fe..8386cd1 100644 (file)
@@ -76,6 +76,8 @@
 #include "LayerSetChromaKeyCommand.h"
 #include "SetOptimizationModeCommand.h"
 #include "GetOptimizationModeCommand.h"
+#include "SetSynchronizedSurfacesCommand.h"
+#include "RemoveSynchronizedSurfacesCommand.h"
 #include <stdbool.h>
 #include <unistd.h>
 #include <stdio.h>
@@ -95,83 +97,85 @@ GenericCommunicator::GenericCommunicator(ICommandExecutor& executor, Configurati
 {
     MethodTable manager_methods[] =
     {
-        { "ServiceConnect",                   &GenericCommunicator::ServiceConnect },
-        { "ServiceDisconnect",                &GenericCommunicator::ServiceDisconnect },
-        { "Debug",                            &GenericCommunicator::Debug },
-        { "ScreenShot",                       &GenericCommunicator::ScreenShot },
-        { "ScreenShotOfLayer",                &GenericCommunicator::ScreenShotOfLayer },
-        { "ScreenShotOfSurface",              &GenericCommunicator::ScreenShotOfSurface },
-        { "GetScreenResolution",              &GenericCommunicator::GetScreenResolution },
-        { "GetNumberOfHardwareLayers",        &GenericCommunicator::GetNumberOfHardwareLayers },
-        { "GetScreenIDs",                     &GenericCommunicator::GetScreenIDs },
-        { "ListAllLayerIDS",                  &GenericCommunicator::ListAllLayerIDS },
-        { "ListAllLayerIDsOnScreen",          &GenericCommunicator::ListAllLayerIDsOnScreen },
-        { "ListAllSurfaceIDS",                &GenericCommunicator::ListAllSurfaceIDS },
-        { "ListSurfaceofLayer",               &GenericCommunicator::ListSurfaceofLayer },
-        { "GetPropertiesOfSurface",           &GenericCommunicator::GetPropertiesOfSurface },
-        { "GetPropertiesOfLayer",             &GenericCommunicator::GetPropertiesOfLayer },
-        { "CreateSurface",                    &GenericCommunicator::CreateSurface },
-        { "CreateSurfaceFromId",              &GenericCommunicator::CreateSurfaceFromId },
-        { "InitializeSurface",                &GenericCommunicator::InitializeSurface },
-        { "InitializeSurfaceFromId",          &GenericCommunicator::InitializeSurfaceFromId },
-        { "SetSurfaceNativeContent",          &GenericCommunicator::SetSurfaceNativeContent },
-        { "RemoveSurfaceNativeContent",       &GenericCommunicator::RemoveSurfaceNativeContent },
-        { "RemoveSurface",                    &GenericCommunicator::RemoveSurface },
-        { "CreateLayer",                      &GenericCommunicator::CreateLayer },
-        { "CreateLayerFromId",                &GenericCommunicator::CreateLayerFromId },
-        { "CreateLayerWithDimension",         &GenericCommunicator::CreateLayerWithDimension },
-        { "CreateLayerFromIdWithDimension",   &GenericCommunicator::CreateLayerFromIdWithDimension },
-        { "RemoveLayer",                      &GenericCommunicator::RemoveLayer },
-        { "AddSurfaceToLayer",                &GenericCommunicator::AddSurfaceToLayer },
-        { "RemoveSurfaceFromLayer",           &GenericCommunicator::RemoveSurfaceFromLayer },
-        { "SetSurfaceSourceRegion",           &GenericCommunicator::SetSurfaceSourceRegion },
-        { "SetLayerSourceRegion",             &GenericCommunicator::SetLayerSourceRegion },
-        { "SetSurfaceDestinationRegion",      &GenericCommunicator::SetSurfaceDestinationRegion },
-        { "SetSurfacePosition",               &GenericCommunicator::SetSurfacePosition },
-        { "GetSurfacePosition",               &GenericCommunicator::GetSurfacePosition },
-        { "SetSurfaceDimension",              &GenericCommunicator::SetSurfaceDimension },
-        { "SetLayerDestinationRegion",        &GenericCommunicator::SetLayerDestinationRegion },
-        { "SetLayerPosition",                 &GenericCommunicator::SetLayerPosition },
-        { "GetLayerPosition",                 &GenericCommunicator::GetLayerPosition },
-        { "SetLayerDimension",                &GenericCommunicator::SetLayerDimension },
-        { "GetLayerDimension",                &GenericCommunicator::GetLayerDimension },
-        { "GetSurfaceDimension",              &GenericCommunicator::GetSurfaceDimension },
-        { "SetSurfaceOpacity",                &GenericCommunicator::SetSurfaceOpacity },
-        { "SetLayerOpacity",                  &GenericCommunicator::SetLayerOpacity },
-        { "GetSurfaceOpacity",                &GenericCommunicator::GetSurfaceOpacity },
-        { "GetLayerOpacity",                  &GenericCommunicator::GetLayerOpacity },
-        { "SetSurfaceOrientation",            &GenericCommunicator::SetSurfaceOrientation },
-        { "GetSurfaceOrientation",            &GenericCommunicator::GetSurfaceOrientation },
-        { "SetLayerOrientation",              &GenericCommunicator::SetLayerOrientation },
-        { "GetLayerOrientation",              &GenericCommunicator::GetLayerOrientation },
-        { "GetSurfacePixelformat",            &GenericCommunicator::GetSurfacePixelformat },
-        { "SetSurfaceVisibility",             &GenericCommunicator::SetSurfaceVisibility },
-        { "SetLayerVisibility",               &GenericCommunicator::SetLayerVisibility },
-        { "GetSurfaceVisibility",             &GenericCommunicator::GetSurfaceVisibility },
-        { "GetLayerVisibility",               &GenericCommunicator::GetLayerVisibility },
-        { "SetRenderOrderOfLayers",           &GenericCommunicator::SetRenderOrderOfLayers },
+        { "ServiceConnect", &GenericCommunicator::ServiceConnect },
+        { "ServiceDisconnect", &GenericCommunicator::ServiceDisconnect },
+        { "Debug", &GenericCommunicator::Debug },
+        { "ScreenShot", &GenericCommunicator::ScreenShot },
+        { "ScreenShotOfLayer", &GenericCommunicator::ScreenShotOfLayer },
+        { "ScreenShotOfSurface", &GenericCommunicator::ScreenShotOfSurface },
+        { "GetScreenResolution", &GenericCommunicator::GetScreenResolution },
+        { "GetNumberOfHardwareLayers", &GenericCommunicator::GetNumberOfHardwareLayers },
+        { "GetScreenIDs", &GenericCommunicator::GetScreenIDs },
+        { "ListAllLayerIDS", &GenericCommunicator::ListAllLayerIDS },
+        { "ListAllLayerIDsOnScreen", &GenericCommunicator::ListAllLayerIDsOnScreen },
+        { "ListAllSurfaceIDS", &GenericCommunicator::ListAllSurfaceIDS },
+        { "ListSurfaceofLayer", &GenericCommunicator::ListSurfaceofLayer },
+        { "GetPropertiesOfSurface", &GenericCommunicator::GetPropertiesOfSurface },
+        { "GetPropertiesOfLayer", &GenericCommunicator::GetPropertiesOfLayer },
+        { "CreateSurface", &GenericCommunicator::CreateSurface },
+        { "CreateSurfaceFromId", &GenericCommunicator::CreateSurfaceFromId },
+        { "InitializeSurface", &GenericCommunicator::InitializeSurface },
+        { "InitializeSurfaceFromId", &GenericCommunicator::InitializeSurfaceFromId },
+        { "SetSurfaceNativeContent", &GenericCommunicator::SetSurfaceNativeContent },
+        { "RemoveSurfaceNativeContent", &GenericCommunicator::RemoveSurfaceNativeContent },
+        { "RemoveSurface", &GenericCommunicator::RemoveSurface },
+        { "CreateLayer", &GenericCommunicator::CreateLayer },
+        { "CreateLayerFromId", &GenericCommunicator::CreateLayerFromId },
+        { "CreateLayerWithDimension", &GenericCommunicator::CreateLayerWithDimension },
+        { "CreateLayerFromIdWithDimension", &GenericCommunicator::CreateLayerFromIdWithDimension },
+        { "RemoveLayer", &GenericCommunicator::RemoveLayer },
+        { "AddSurfaceToLayer", &GenericCommunicator::AddSurfaceToLayer },
+        { "RemoveSurfaceFromLayer", &GenericCommunicator::RemoveSurfaceFromLayer },
+        { "SetSurfaceSourceRegion", &GenericCommunicator::SetSurfaceSourceRegion },
+        { "SetLayerSourceRegion", &GenericCommunicator::SetLayerSourceRegion },
+        { "SetSurfaceDestinationRegion", &GenericCommunicator::SetSurfaceDestinationRegion },
+        { "SetSurfacePosition", &GenericCommunicator::SetSurfacePosition },
+        { "GetSurfacePosition", &GenericCommunicator::GetSurfacePosition },
+        { "SetSurfaceDimension", &GenericCommunicator::SetSurfaceDimension },
+        { "SetLayerDestinationRegion", &GenericCommunicator::SetLayerDestinationRegion },
+        { "SetLayerPosition", &GenericCommunicator::SetLayerPosition },
+        { "GetLayerPosition", &GenericCommunicator::GetLayerPosition },
+        { "SetLayerDimension", &GenericCommunicator::SetLayerDimension },
+        { "GetLayerDimension", &GenericCommunicator::GetLayerDimension },
+        { "GetSurfaceDimension", &GenericCommunicator::GetSurfaceDimension },
+        { "SetSurfaceOpacity", &GenericCommunicator::SetSurfaceOpacity },
+        { "SetLayerOpacity", &GenericCommunicator::SetLayerOpacity },
+        { "GetSurfaceOpacity", &GenericCommunicator::GetSurfaceOpacity },
+        { "GetLayerOpacity", &GenericCommunicator::GetLayerOpacity },
+        { "SetSurfaceOrientation", &GenericCommunicator::SetSurfaceOrientation },
+        { "GetSurfaceOrientation", &GenericCommunicator::GetSurfaceOrientation },
+        { "SetLayerOrientation", &GenericCommunicator::SetLayerOrientation },
+        { "GetLayerOrientation", &GenericCommunicator::GetLayerOrientation },
+        { "GetSurfacePixelformat", &GenericCommunicator::GetSurfacePixelformat },
+        { "SetSurfaceVisibility", &GenericCommunicator::SetSurfaceVisibility },
+        { "SetLayerVisibility", &GenericCommunicator::SetLayerVisibility },
+        { "GetSurfaceVisibility", &GenericCommunicator::GetSurfaceVisibility },
+        { "GetLayerVisibility", &GenericCommunicator::GetLayerVisibility },
+        { "SetRenderOrderOfLayers", &GenericCommunicator::SetRenderOrderOfLayers },
         { "SetSurfaceRenderOrderWithinLayer", &GenericCommunicator::SetSurfaceRenderOrderWithinLayer },
-        { "GetLayerType",                     &GenericCommunicator::GetLayerType },
-        { "GetLayertypeCapabilities",         &GenericCommunicator::GetLayertypeCapabilities },
-        { "GetLayerCapabilities",             &GenericCommunicator::GetLayerCapabilities },
-        { "Exit",                             &GenericCommunicator::Exit },
-        { "CommitChanges",                    &GenericCommunicator::CommitChanges },
-        { "CreateShader",                     &GenericCommunicator::CreateShader },
-        { "DestroyShader",                    &GenericCommunicator::DestroyShader },
-        { "SetShader",                        &GenericCommunicator::SetShader },
-        { "SetUniforms",                      &GenericCommunicator::SetUniforms },
-        { "SetKeyboardFocusOn",               &GenericCommunicator::SetKeyboardFocusOn },
-        { "GetKeyboardFocusSurfaceId",        &GenericCommunicator::GetKeyboardFocusSurfaceId },
-        { "UpdateInputEventAcceptanceOn",     &GenericCommunicator::UpdateInputEventAcceptanceOn },
-        { "SetSurfaceChromaKey",              &GenericCommunicator::SetSurfaceChromaKey },
-        { "SetLayerChromaKey",                &GenericCommunicator::SetLayerChromaKey },
-        { "LayerAddNotification",             &GenericCommunicator::LayerAddNotification },
-        { "SurfaceAddNotification",           &GenericCommunicator::SurfaceAddNotification },
-        { "LayerRemoveNotification",          &GenericCommunicator::LayerRemoveNotification },
-        { "SurfaceRemoveNotification",        &GenericCommunicator::SurfaceRemoveNotification },
-        { "SetOptimizationMode",              &GenericCommunicator::SetOptimizationMode },
-        { "GetOptimizationMode",              &GenericCommunicator::GetOptimizationMode },
-        { "GetPropertiesOfScreen",            &GenericCommunicator::GetPropertiesOfScreen }
+        { "GetLayerType", &GenericCommunicator::GetLayerType },
+        { "GetLayertypeCapabilities", &GenericCommunicator::GetLayertypeCapabilities },
+        { "GetLayerCapabilities", &GenericCommunicator::GetLayerCapabilities },
+        { "Exit", &GenericCommunicator::Exit },
+        { "CommitChanges", &GenericCommunicator::CommitChanges },
+        { "CreateShader", &GenericCommunicator::CreateShader },
+        { "DestroyShader", &GenericCommunicator::DestroyShader },
+        { "SetShader", &GenericCommunicator::SetShader },
+        { "SetUniforms", &GenericCommunicator::SetUniforms },
+        { "SetKeyboardFocusOn", &GenericCommunicator::SetKeyboardFocusOn },
+        { "GetKeyboardFocusSurfaceId", &GenericCommunicator::GetKeyboardFocusSurfaceId },
+        { "UpdateInputEventAcceptanceOn", &GenericCommunicator::UpdateInputEventAcceptanceOn },
+        { "SetSurfaceChromaKey", &GenericCommunicator::SetSurfaceChromaKey },
+        { "SetLayerChromaKey", &GenericCommunicator::SetLayerChromaKey },
+        { "LayerAddNotification", &GenericCommunicator::LayerAddNotification },
+        { "SurfaceAddNotification", &GenericCommunicator::SurfaceAddNotification },
+        { "LayerRemoveNotification", &GenericCommunicator::LayerRemoveNotification },
+        { "SurfaceRemoveNotification", &GenericCommunicator::SurfaceRemoveNotification },
+        { "SetOptimizationMode", &GenericCommunicator::SetOptimizationMode },
+        { "GetOptimizationMode", &GenericCommunicator::GetOptimizationMode },
+        { "GetPropertiesOfScreen", &GenericCommunicator::GetPropertiesOfScreen },
+        { "SetSynchronizedSurfaces", &GenericCommunicator::SetSynchronizedSurfaces },
+        { "RemoveSynchronizedSurfaces", &GenericCommunicator::RemoveSynchronizedSurfaces }
     };
 
     int entryCount = sizeof(manager_methods) / sizeof(MethodTable);
@@ -221,10 +225,10 @@ bool GenericCommunicator::start()
 
 void GenericCommunicator::stop()
 {
-    LOG_INFO("GenericCommunicator","stopping");
+    LOG_INFO("GenericCommunicator", "stopping");
 
     threadStop();
-    
+
     if (m_running)
     {
         m_ipcModule.destroy();
@@ -244,37 +248,37 @@ void GenericCommunicator::process(int timeout_ms)
     t_ilm_const_string name = m_ipcModule.getMessageName(message);
     t_ilm_client_handle senderHandle = m_ipcModule.getSenderHandle(message);
 
-    switch(messageType)
+    switch (messageType)
     {
     case IpcMessageTypeCommand:
         if (m_callBackTable.end() != m_callBackTable.find(name))
         {
             LOG_DEBUG("GenericCommunicator", "received: " << name << " from "
-                       << m_executor->getSenderName(senderHandle)
-                       << "(" << m_executor->getSenderPid(senderHandle) << ")");
+                        << m_executor->getSenderName(senderHandle)
+                        << "(" << m_executor->getSenderPid(senderHandle) << ")");
             CallBackMethod method = m_callBackTable[name].function;
             (this->*method)(message);
         }
         else
         {
             LOG_WARNING("GenericCommunicator", "Received unknown command " << name
-                  << " from " << m_executor->getSenderName(senderHandle)
-                  << "(pid " << m_executor->getSenderPid(senderHandle) << ")");
+                    << " from " << m_executor->getSenderName(senderHandle)
+                    << "(pid " << m_executor->getSenderPid(senderHandle) << ")");
         }
         processNotificationQueue();
         break;
 
     case IpcMessageTypeConnect:
         LOG_DEBUG("GenericCommunicator", "client " << m_executor->getSenderName(senderHandle)
-                  << "(pid " << m_executor->getSenderPid(senderHandle) << ") connected");
+                    << "(pid " << m_executor->getSenderPid(senderHandle) << ") connected");
         break;
 
     case IpcMessageTypeDisconnect:
         LOG_DEBUG("GenericCommunicator", "client " << m_executor->getSenderName(senderHandle)
-                  << "(pid " << m_executor->getSenderPid(senderHandle) << ") disconnected");
+                    << "(pid " << m_executor->getSenderPid(senderHandle) << ") disconnected");
         {
             const LayerMap& layers = m_executor->getScene()->getAllLayers();
-            LayerMapConstIterator layerIter =  layers.begin();
+            LayerMapConstIterator layerIter = layers.begin();
             LayerMapConstIterator layerIterEnd = layers.end();
             for (; layerIter != layerIterEnd; ++layerIter)
             {
@@ -283,20 +287,21 @@ void GenericCommunicator::process(int timeout_ms)
             }
 
             const SurfaceMap& surfaces = m_executor->getScene()->getAllSurfaces();
-            SurfaceMapConstIterator surfaceIter =  surfaces.begin();
+            SurfaceMapConstIterator surfaceIter = surfaces.begin();
             SurfaceMapConstIterator surfaceIterEnd = surfaces.end();
             for (; surfaceIter != surfaceIterEnd; ++surfaceIter)
             {
                 Surface* surface = surfaceIter->second;
                 surface->removeNotification(senderHandle);
             }
+            m_executor->removeApplicationReference(senderHandle);
         }
         break;
 
     case IpcMessageTypeError:
         LOG_DEBUG("GenericCommunicator", "Received error message " << name << " from "
-                  << m_executor->getSenderName(senderHandle)
-                  << "(pid " << m_executor->getSenderPid(senderHandle) << ")");
+                    << m_executor->getSenderName(senderHandle)
+                    << "(pid " << m_executor->getSenderPid(senderHandle) << ")");
         break;
 
     case IpcMessageTypeShutdown:
@@ -305,8 +310,8 @@ void GenericCommunicator::process(int timeout_ms)
 
     default:
         LOG_DEBUG("GenericCommunicator", "Received unknown data from "
-                  << m_executor->getSenderName(senderHandle)
-                  << "(pid " << m_executor->getSenderPid(senderHandle) << "), Message type: " << messageType);
+                    << m_executor->getSenderName(senderHandle)
+                    << "(pid " << m_executor->getSenderPid(senderHandle) << "), Message type: " << messageType);
         break;
     }
     m_ipcModule.destroyMessage(message);
@@ -341,8 +346,8 @@ void GenericCommunicator::ServiceConnect(t_ilm_message message)
     m_executor->addApplicationReference(clientHandle, new IApplicationReference(processName, processId));
 
     LOG_DEBUG("GenericCommunicator", "ServiceConnect called from "
-              << m_executor->getSenderName(clientHandle)
-              << "(" << m_executor->getSenderPid(clientHandle) << ")");
+                << m_executor->getSenderName(clientHandle)
+                << "(" << m_executor->getSenderPid(clientHandle) << ")");
 
     response = m_ipcModule.createResponse(message);
     m_ipcModule.sendToClients(response, &clientHandle, 1);
@@ -355,8 +360,8 @@ void GenericCommunicator::ServiceDisconnect(t_ilm_message message)
     t_ilm_client_handle clientHandle = m_ipcModule.getSenderHandle(message);
 
     LOG_DEBUG("GenericCommunicator", "ServiceDisconnect called from "
-              << m_executor->getSenderName(clientHandle)
-              << "(" << m_executor->getSenderPid(clientHandle) << ")");
+                << m_executor->getSenderName(clientHandle)
+                << "(" << m_executor->getSenderPid(clientHandle) << ")");
 
     m_executor->removeApplicationReference(clientHandle);
 
@@ -401,6 +406,8 @@ void GenericCommunicator::GetScreenResolution(t_ilm_message message)
     m_ipcModule.appendUint(response, resolution[1]);
     m_ipcModule.sendToClients(response, &clientHandle, 1);
     m_ipcModule.destroyMessage(response);
+
+    delete [] resolution;
 }
 
 void GenericCommunicator::GetNumberOfHardwareLayers(t_ilm_message message)
@@ -426,6 +433,7 @@ void GenericCommunicator::GetScreenIDs(t_ilm_message message)
     m_ipcModule.appendUintArray(response, IDs, length);
     m_ipcModule.sendToClients(response, &clientHandle, 1);
     m_ipcModule.destroyMessage(response);
+    delete [] IDs;
 }
 
 void GenericCommunicator::ScreenShot(t_ilm_message message)
@@ -543,6 +551,8 @@ void GenericCommunicator::ListAllLayerIDsOnScreen(t_ilm_message message)
 
     m_ipcModule.sendToClients(response, &clientHandle, 1);
     m_ipcModule.destroyMessage(response);
+
+    delete [] array;
 }
 
 void GenericCommunicator::ListAllSurfaceIDS(t_ilm_message message)
@@ -586,6 +596,7 @@ void GenericCommunicator::ListSurfaceofLayer(t_ilm_message message)
 
         response = m_ipcModule.createResponse(message);
         m_ipcModule.appendUintArray(response, array, length);
+        delete [] array;
     }
     else
     {
@@ -720,7 +731,6 @@ void GenericCommunicator::CreateSurface(t_ilm_message message)
 
     pf = (PixelFormat) pixelformat;
 
-
     // First of all create the surface
     t_ilm_bool status = m_executor->execute(new SurfaceCreateCommand(clientPid, &id));
 
@@ -957,6 +967,8 @@ void GenericCommunicator::CreateLayerFromId(t_ilm_message message)
 
     m_ipcModule.sendToClients(response, &clientHandle, 1);
     m_ipcModule.destroyMessage(response);
+
+    delete [] resolution;
 }
 
 
@@ -1662,7 +1674,7 @@ void GenericCommunicator::GetSurfacePixelformat(t_ilm_message message)
     t_ilm_client_handle clientHandle = m_ipcModule.getSenderHandle(message);
     t_ilm_uint clientPid = m_executor->getSenderPid(clientHandle);
     uint id = 0;
-    PixelFormat pixelFormat;
+    PixelFormat pixelFormat = (PixelFormat)ILM_PIXEL_FORMAT_UNKNOWN;
 
     m_ipcModule.getUint(message, &id);
 
@@ -1740,7 +1752,7 @@ void GenericCommunicator::GetSurfaceVisibility(t_ilm_message message)
     t_ilm_client_handle clientHandle = m_ipcModule.getSenderHandle(message);
     t_ilm_uint clientPid = m_executor->getSenderPid(clientHandle);
     uint id = 0;
-    bool visibility;
+    bool visibility = false;
 
     m_ipcModule.getUint(message, &id);
 
@@ -1766,7 +1778,7 @@ void GenericCommunicator::GetLayerVisibility(t_ilm_message message)
     t_ilm_client_handle clientHandle = m_ipcModule.getSenderHandle(message);
     t_ilm_uint clientPid = m_executor->getSenderPid(clientHandle);
     uint id = 0;
-    bool visibility;
+    bool visibility = false;
 
     m_ipcModule.getUint(message, &id);
 
@@ -1786,16 +1798,92 @@ void GenericCommunicator::GetLayerVisibility(t_ilm_message message)
     m_ipcModule.destroyMessage(response);
 }
 
+void GenericCommunicator::SetSynchronizedSurfaces(t_ilm_message message)
+{
+    t_ilm_message response;
+    t_ilm_client_handle clientHandle = m_ipcModule.getSenderHandle(message);
+    t_ilm_uint clientPid = m_executor->getSenderPid(clientHandle);
+
+    // ipcArray was created in ipcModule using malloc (it's implemented C)
+    // so we copy it to a buffer created with new() and discard
+    // the ipcArray using free() to avoid memory corruption
+    uint* ipcArray = NULL;
+    uint* array = NULL;
+    int length = 0;
+    m_ipcModule.getUintArray(message, &ipcArray, &length);
+    array = new uint[length];
+    memset(array, 0, length * sizeof(uint));
+    memcpy(array, ipcArray, length * sizeof(uint));
+    free(ipcArray);
+
+    t_ilm_bool status = m_executor->execute(new SetSynchronizedSurfacesCommand(clientPid, array, length));
+    if (status)
+    {
+        response = m_ipcModule.createResponse(message);
+    }
+    else
+    {
+        response = m_ipcModule.createErrorResponse(message);
+        m_ipcModule.appendUint(response, ILM_ERROR_RESOURCE_NOT_FOUND);
+    }
+
+    m_ipcModule.sendToClients(response, &clientHandle, 1);
+    m_ipcModule.destroyMessage(response);
+}
+
+void GenericCommunicator::RemoveSynchronizedSurfaces(t_ilm_message message)
+{
+    t_ilm_message response;
+    t_ilm_client_handle clientHandle = m_ipcModule.getSenderHandle(message);
+    t_ilm_uint clientPid = m_executor->getSenderPid(clientHandle);
+
+    // ipcArray was created in ipcModule using malloc (it's implemented C)
+    // so we copy it to a buffer created with new() and discard
+    // the ipcArray using free() to avoid memory corruption
+    uint* ipcArray = NULL;
+    uint* array = NULL;
+    int length = 0;
+    m_ipcModule.getUintArray(message, &ipcArray, &length);
+    array = new uint[length];
+    memset(array, 0, length * sizeof(uint));
+    memcpy(array, ipcArray, length * sizeof(uint));
+    free(ipcArray);
+
+    t_ilm_bool status = m_executor->execute(new RemoveSynchronizedSurfacesCommand(clientPid, array, length));
+    if (status)
+    {
+        response = m_ipcModule.createResponse(message);
+    }
+    else
+    {
+        response = m_ipcModule.createErrorResponse(message);
+        m_ipcModule.appendUint(response, ILM_ERROR_RESOURCE_NOT_FOUND);
+    }
+
+    m_ipcModule.sendToClients(response, &clientHandle, 1);
+    m_ipcModule.destroyMessage(response);
+}
+
 void GenericCommunicator::SetRenderOrderOfLayers(t_ilm_message message)
 {
     t_ilm_message response;
     t_ilm_client_handle clientHandle = m_ipcModule.getSenderHandle(message);
     t_ilm_uint clientPid = m_executor->getSenderPid(clientHandle);
+
+    // ipcArray was created in ipcModule using malloc (it's implemented C)
+    // so we copy it to a buffer created with new() and discard
+    // the ipcArray using free() to avoid memory corruption
+    uint* ipcArray = NULL;
     uint* array = NULL;
     int length = 0;
+    m_ipcModule.getUintArray(message, &ipcArray, &length);
+    array = new uint[length];
+    memset(array, 0, length * sizeof(uint));
+    memcpy(array, ipcArray, length * sizeof(uint));
+    free(ipcArray);
+
     uint screenID = 0;
 
-    m_ipcModule.getUintArray(message, &array, &length);
     m_ipcModule.getUint(message, &screenID);
 
     t_ilm_bool status = m_executor->execute(new ScreenSetRenderOrderCommand(clientPid, screenID, array, length));
@@ -1818,12 +1906,21 @@ void GenericCommunicator::SetSurfaceRenderOrderWithinLayer(t_ilm_message message
     t_ilm_message response;
     t_ilm_client_handle clientHandle = m_ipcModule.getSenderHandle(message);
     t_ilm_uint clientPid = m_executor->getSenderPid(clientHandle);
-    uint* array = NULL;
-    int length = 0;
-    uint layerid = 0;
 
+    uint layerid = 0;
     m_ipcModule.getUint(message, &layerid);
-    m_ipcModule.getUintArray(message, &array, &length);
+
+    // ipcArray was created in ipcModule using malloc (it's implemented C)
+    // so we copy it to a buffer created with new() and discard
+    // the ipcArray using free() to avoid memory corruption
+    uint* ipcArray = NULL;
+    uint* array = NULL;
+    int length = 0;
+    m_ipcModule.getUintArray(message, &ipcArray, &length);
+    array = new uint[length];
+    memset(array, 0, length * sizeof(uint));
+    memcpy(array, ipcArray, length * sizeof(uint));
+    free(ipcArray);
 
     t_ilm_bool status = m_executor->execute(new LayerSetRenderOrderCommand(clientPid, layerid, array, length));
     if (status)
@@ -2330,149 +2427,149 @@ void GenericCommunicator::sendNotification(GraphicalObject* object, t_ilm_notifi
     switch (object->type)
     {
     case TypeLayer:
-    {
-        Layer* layer = static_cast<Layer*>(object);
-        if (layer)
         {
-            ApplicationReferenceList& arl = layer->getNotificationClients();
-
-            if (arl.size())
+            Layer* layer = static_cast<Layer*>(object);
+            if (layer)
             {
-                t_ilm_message notification;
-                Rectangle dest = layer->getDestinationRegion();
-                Rectangle src = layer->getSourceRegion();
-                OrientationType orientation = layer->getOrientation();
-
-                unsigned char chromaKeyRed = 0;
-                unsigned char chromaKeyGreen = 0;
-                unsigned char chromaKeyBlue = 0;
-                layer->getChromaKey(chromaKeyRed, chromaKeyGreen, chromaKeyBlue);
-
-                std::stringstream notificationName;
-                notificationName << "NotificationForLayer" << layer->getID();
-
-                notification = m_ipcModule.createNotification(notificationName.str().c_str());
-                m_ipcModule.appendUint(notification, layer->getID());
-                m_ipcModule.appendUint(notification, mask);
-
-                m_ipcModule.appendDouble(notification, layer->getOpacity());
-                m_ipcModule.appendUint(notification, src.x);
-                m_ipcModule.appendUint(notification, src.y);
-                m_ipcModule.appendUint(notification, src.width);
-                m_ipcModule.appendUint(notification, src.height);
-                m_ipcModule.appendUint(notification, layer->OriginalSourceWidth);
-                m_ipcModule.appendUint(notification, layer->OriginalSourceHeight);
-                m_ipcModule.appendUint(notification, dest.x);
-                m_ipcModule.appendUint(notification, dest.y);
-                m_ipcModule.appendUint(notification, dest.width);
-                m_ipcModule.appendUint(notification, dest.height);
-                m_ipcModule.appendUint(notification, orientation);
-                m_ipcModule.appendBool(notification, layer->getVisibility());
-                m_ipcModule.appendUint(notification, layer->getLayerType());
-                m_ipcModule.appendBool(notification, layer->getChromaKeyEnabled());
-                m_ipcModule.appendUint(notification, chromaKeyRed);
-                m_ipcModule.appendUint(notification, chromaKeyGreen);
-                m_ipcModule.appendUint(notification, chromaKeyBlue);
-                m_ipcModule.appendInt(notification, layer->getCreatorPid());
-
-                int clientCount = arl.size();
-                t_ilm_client_handle clientArray[256];
-
-                ApplicationReferenceList::iterator iter = arl.begin();
-                ApplicationReferenceList::iterator end = arl.end();
-
-                for (int clientNumber = 0;
-                     iter != end, clientNumber < 256;
-                     ++iter, ++clientNumber)
-                {
-                    t_ilm_client_handle client = *iter;
-                    clientArray[clientNumber] = client;
-                }
-
-                LOG_DEBUG("GenericCommunicator", "Sending " << clientCount << " notification(s): layer " << layer->getID() << " was updated.");
+                ApplicationReferenceList& arl = layer->getNotificationClients();
 
-                if (!m_ipcModule.sendToClients(notification, clientArray, clientCount))
+                if (arl.size())
                 {
-                    LOG_ERROR("GenericCommunicator", "Sending notification to clients failed.")
+                    t_ilm_message notification;
+                    Rectangle dest = layer->getDestinationRegion();
+                    Rectangle src = layer->getSourceRegion();
+                    OrientationType orientation = layer->getOrientation();
+
+                    unsigned char chromaKeyRed = 0;
+                    unsigned char chromaKeyGreen = 0;
+                    unsigned char chromaKeyBlue = 0;
+                    layer->getChromaKey(chromaKeyRed, chromaKeyGreen, chromaKeyBlue);
+
+                    std::stringstream notificationName;
+                    notificationName << "NotificationForLayer" << layer->getID();
+
+                    notification = m_ipcModule.createNotification(notificationName.str().c_str());
+                    m_ipcModule.appendUint(notification, layer->getID());
+                    m_ipcModule.appendUint(notification, mask);
+
+                    m_ipcModule.appendDouble(notification, layer->getOpacity());
+                    m_ipcModule.appendUint(notification, src.x);
+                    m_ipcModule.appendUint(notification, src.y);
+                    m_ipcModule.appendUint(notification, src.width);
+                    m_ipcModule.appendUint(notification, src.height);
+                    m_ipcModule.appendUint(notification, layer->OriginalSourceWidth);
+                    m_ipcModule.appendUint(notification, layer->OriginalSourceHeight);
+                    m_ipcModule.appendUint(notification, dest.x);
+                    m_ipcModule.appendUint(notification, dest.y);
+                    m_ipcModule.appendUint(notification, dest.width);
+                    m_ipcModule.appendUint(notification, dest.height);
+                    m_ipcModule.appendUint(notification, orientation);
+                    m_ipcModule.appendBool(notification, layer->getVisibility());
+                    m_ipcModule.appendUint(notification, layer->getLayerType());
+                    m_ipcModule.appendBool(notification, layer->getChromaKeyEnabled());
+                    m_ipcModule.appendUint(notification, chromaKeyRed);
+                    m_ipcModule.appendUint(notification, chromaKeyGreen);
+                    m_ipcModule.appendUint(notification, chromaKeyBlue);
+                    m_ipcModule.appendInt(notification, layer->getCreatorPid());
+
+                    int clientCount = arl.size();
+                    t_ilm_client_handle clientArray[256];
+
+                    ApplicationReferenceList::iterator iter = arl.begin();
+                    ApplicationReferenceList::iterator end = arl.end();
+
+                    for (int clientNumber = 0;
+                            iter != end, clientNumber < 256;
+                            ++iter, ++clientNumber)
+                    {
+                        t_ilm_client_handle client = *iter;
+                        clientArray[clientNumber] = client;
+                    }
+
+                    LOG_DEBUG("GenericCommunicator", "Sending " << clientCount << " notification(s): layer " << layer->getID() << " was updated.");
+
+                    if (!m_ipcModule.sendToClients(notification, clientArray, clientCount))
+                    {
+                        LOG_ERROR("GenericCommunicator", "Sending notification to clients failed.")
+                    }
+
+                    m_ipcModule.destroyMessage(notification);
                 }
-
-                m_ipcModule.destroyMessage(notification);
             }
         }
-    }
-    break;
+        break;
     case TypeSurface:
-    {
-        Surface* surface = static_cast<Surface*>(object);
-        if (surface)
         {
-            ApplicationReferenceList& arl = surface->getNotificationClients();
-
-            if (arl.size())
+            Surface* surface = static_cast<Surface*>(object);
+            if (surface)
             {
-                t_ilm_message notification;
-                std::stringstream notificationName;
-                notificationName << "NotificationForSurface" << surface->getID();
-
-                unsigned char chromaKeyRed = 0;
-                unsigned char chromaKeyGreen = 0;
-                unsigned char chromaKeyBlue = 0;
-                surface->getChromaKey(chromaKeyRed, chromaKeyGreen, chromaKeyBlue);
-
-                notification = m_ipcModule.createNotification(notificationName.str().c_str());
-                m_ipcModule.appendUint(notification, surface->getID());
-                m_ipcModule.appendUint(notification, mask);
-
-                m_ipcModule.appendDouble(notification, surface->getOpacity());
-                m_ipcModule.appendUint(notification, surface->getSourceRegion().x);
-                m_ipcModule.appendUint(notification, surface->getSourceRegion().y);
-                m_ipcModule.appendUint(notification, surface->getSourceRegion().width);
-                m_ipcModule.appendUint(notification, surface->getSourceRegion().height);
-                m_ipcModule.appendUint(notification, surface->OriginalSourceWidth);
-                m_ipcModule.appendUint(notification, surface->OriginalSourceHeight);
-                m_ipcModule.appendUint(notification, surface->getDestinationRegion().x);
-                m_ipcModule.appendUint(notification, surface->getDestinationRegion().y);
-                m_ipcModule.appendUint(notification, surface->getDestinationRegion().width);
-                m_ipcModule.appendUint(notification, surface->getDestinationRegion().height);
-                m_ipcModule.appendUint(notification, surface->getOrientation());
-                m_ipcModule.appendBool(notification, surface->getVisibility());
-                m_ipcModule.appendUint(notification, surface->frameCounter);
-                m_ipcModule.appendUint(notification, surface->drawCounter);
-                m_ipcModule.appendUint(notification, surface->updateCounter);
-                m_ipcModule.appendUint(notification, surface->getPixelFormat());
-                m_ipcModule.appendUint(notification, surface->getNativeContent());
-                m_ipcModule.appendUint(notification, surface->getInputEventAcceptanceOnDevices());
-                m_ipcModule.appendBool(notification, surface->getChromaKeyEnabled());
-                m_ipcModule.appendUint(notification, chromaKeyRed);
-                m_ipcModule.appendUint(notification, chromaKeyGreen);
-                m_ipcModule.appendUint(notification, chromaKeyBlue);
-                m_ipcModule.appendInt(notification, surface->getCreatorPid());
-
-                int clientCount = arl.size();
-                t_ilm_client_handle clients[256];
-
-                ApplicationReferenceList::iterator iter = arl.begin();
-                ApplicationReferenceList::iterator end = arl.end();
-
-                for (int clientNumber = 0;
-                     iter != end, clientNumber < 256;
-                     ++iter, ++clientNumber)
-                {
-                    clients[clientNumber] = *iter;
-                }
-
-                LOG_DEBUG("GenericCommunicator", "Sending " << clientCount << " notification(s): surface " << surface->getID() << " was updated.");
+                ApplicationReferenceList& arl = surface->getNotificationClients();
 
-                if (!m_ipcModule.sendToClients(notification, clients, clientCount))
+                if (arl.size())
                 {
-                    LOG_ERROR("GenericCommunicator", "Sending notification to clients failed.")
+                    t_ilm_message notification;
+                    std::stringstream notificationName;
+                    notificationName << "NotificationForSurface" << surface->getID();
+
+                    unsigned char chromaKeyRed = 0;
+                    unsigned char chromaKeyGreen = 0;
+                    unsigned char chromaKeyBlue = 0;
+                    surface->getChromaKey(chromaKeyRed, chromaKeyGreen, chromaKeyBlue);
+
+                    notification = m_ipcModule.createNotification(notificationName.str().c_str());
+                    m_ipcModule.appendUint(notification, surface->getID());
+                    m_ipcModule.appendUint(notification, mask);
+
+                    m_ipcModule.appendDouble(notification, surface->getOpacity());
+                    m_ipcModule.appendUint(notification, surface->getSourceRegion().x);
+                    m_ipcModule.appendUint(notification, surface->getSourceRegion().y);
+                    m_ipcModule.appendUint(notification, surface->getSourceRegion().width);
+                    m_ipcModule.appendUint(notification, surface->getSourceRegion().height);
+                    m_ipcModule.appendUint(notification, surface->OriginalSourceWidth);
+                    m_ipcModule.appendUint(notification, surface->OriginalSourceHeight);
+                    m_ipcModule.appendUint(notification, surface->getDestinationRegion().x);
+                    m_ipcModule.appendUint(notification, surface->getDestinationRegion().y);
+                    m_ipcModule.appendUint(notification, surface->getDestinationRegion().width);
+                    m_ipcModule.appendUint(notification, surface->getDestinationRegion().height);
+                    m_ipcModule.appendUint(notification, surface->getOrientation());
+                    m_ipcModule.appendBool(notification, surface->getVisibility());
+                    m_ipcModule.appendUint(notification, surface->frameCounter);
+                    m_ipcModule.appendUint(notification, surface->drawCounter);
+                    m_ipcModule.appendUint(notification, surface->updateCounter);
+                    m_ipcModule.appendUint(notification, surface->getPixelFormat());
+                    m_ipcModule.appendUint(notification, surface->getNativeContent());
+                    m_ipcModule.appendUint(notification, surface->getInputEventAcceptanceOnDevices());
+                    m_ipcModule.appendBool(notification, surface->getChromaKeyEnabled());
+                    m_ipcModule.appendUint(notification, chromaKeyRed);
+                    m_ipcModule.appendUint(notification, chromaKeyGreen);
+                    m_ipcModule.appendUint(notification, chromaKeyBlue);
+                    m_ipcModule.appendInt(notification, surface->getCreatorPid());
+
+                    int clientCount = arl.size();
+                    t_ilm_client_handle clients[256];
+
+                    ApplicationReferenceList::iterator iter = arl.begin();
+                    ApplicationReferenceList::iterator end = arl.end();
+
+                    for (int clientNumber = 0;
+                            iter != end, clientNumber < 256;
+                            ++iter, ++clientNumber)
+                    {
+                        clients[clientNumber] = *iter;
+                    }
+
+                    LOG_DEBUG("GenericCommunicator", "Sending " << clientCount << " notification(s): surface " << surface->getID() << " was updated.");
+
+                    if (!m_ipcModule.sendToClients(notification, clients, clientCount))
+                    {
+                        LOG_ERROR("GenericCommunicator", "Sending notification to clients failed.")
+                    }
+
+                    m_ipcModule.destroyMessage(notification);
                 }
-
-                m_ipcModule.destroyMessage(notification);
             }
         }
-    }
-    break;
+        break;
     default:
         LOG_INFO("GenericCommunicator", "Unknown notification found in queue.");
         break;
@@ -2494,26 +2591,26 @@ void GenericCommunicator::SetOptimizationMode(t_ilm_message message)
     t_ilm_message response;
     OptimizationType optimizationId;
     OptimizationModeType optimizationMode;
-    unsigned int o;    
+    unsigned int optMode;
     t_ilm_client_handle clientHandle = m_ipcModule.getSenderHandle(message);
     t_ilm_uint clientPid = m_executor->getSenderPid(clientHandle);
 
-    m_ipcModule.getUint(message,&o);
-    optimizationId = (OptimizationType) o;
-    m_ipcModule.getUint(message,&o);
-    optimizationMode = (OptimizationModeType) o;
+    m_ipcModule.getUint(message, &optMode);
+    optimizationId = (OptimizationType)optMode;
+    m_ipcModule.getUint(message, &optMode);
+    optimizationMode = (OptimizationModeType)optMode;
 
     t_ilm_bool status = m_executor->execute(new SetOptimizationModeCommand(clientPid, optimizationId, optimizationMode));
     if (status)
     {
-        response = m_ipcModule.createResponse(message);       
+        response = m_ipcModule.createResponse(message);
     }
     else
     {
         response = m_ipcModule.createErrorResponse(message);
         m_ipcModule.appendUint(response, ILM_ERROR_RESOURCE_NOT_FOUND);
     }
-    m_ipcModule.sendToClients(response,&clientHandle,1);
+    m_ipcModule.sendToClients(response, &clientHandle, 1);
     m_ipcModule.destroyMessage(response);
 }
 
@@ -2526,24 +2623,22 @@ void GenericCommunicator::GetOptimizationMode(t_ilm_message message)
     OptimizationModeType optimizationMode;
     unsigned int o;
 
-    m_ipcModule.getUint(message,&o);
+    m_ipcModule.getUint(message, &o);
     optimizationId = (OptimizationType)o;
 
     t_ilm_bool status = m_executor->execute(new GetOptimizationModeCommand(clientPid, optimizationId, &optimizationMode));
     if (status)
     {
-        
         response = m_ipcModule.createResponse(message);
-        m_ipcModule.appendUint(response,(unsigned int)optimizationMode);
+        m_ipcModule.appendUint(response, (unsigned int)optimizationMode);
     }
     else
     {
         response = m_ipcModule.createErrorResponse(message);
         m_ipcModule.appendUint(response, ILM_ERROR_RESOURCE_NOT_FOUND);
     }
-    m_ipcModule.sendToClients(response,&clientHandle,1);
+    m_ipcModule.sendToClients(response, &clientHandle, 1);
     m_ipcModule.destroyMessage(response);
-
 }
 
 void GenericCommunicator::GetPropertiesOfScreen(t_ilm_message message)
@@ -2571,7 +2666,7 @@ void GenericCommunicator::GetPropertiesOfScreen(t_ilm_message message)
 
         response = m_ipcModule.createResponse(message);
         m_ipcModule.appendUintArray(response, layerIdVector.data(), layerIdVector.size());
-        m_ipcModule.appendUint(response,numberOfHardwareLayers);
+        m_ipcModule.appendUint(response, numberOfHardwareLayers);
         m_ipcModule.appendUint(response, resolution[0]);
         m_ipcModule.appendUint(response, resolution[1]);
     }