[gimp] plug-ins: properly free rectScreens after allocating it.



commit 9fb1c2868709d1012068756f0682d081537d132c
Author: Jehan <jehan girinstud io>
Date:   Wed May 16 00:24:13 2018 +0200

    plug-ins: properly free rectScreens after allocating it.
    
    Also avoid global variables when possible. We can just use the data
    variable of EnumDisplayMonitors() which will be passed on to the
    callback. This is not perfect yet since rectScreensCount is still
    global, but let's go for it for now.

 plug-ins/screenshot/screenshot-win32.c |   95 +++++++++++++++++---------------
 1 files changed, 50 insertions(+), 45 deletions(-)
---
diff --git a/plug-ins/screenshot/screenshot-win32.c b/plug-ins/screenshot/screenshot-win32.c
index dcf8639..a11ca68 100644
--- a/plug-ins/screenshot/screenshot-win32.c
+++ b/plug-ins/screenshot/screenshot-win32.c
@@ -77,7 +77,6 @@ static HINSTANCE       hInst = NULL;
 static HCURSOR         selectCursor = 0;
 static ICONINFO        iconInfo;
 static MAGIMAGEHEADER  returnedSrcheader;
-static RECT           *rectScreens = NULL;
 static int             rectScreensCount = 0;
 
 static gint32    *image_id;
@@ -632,20 +631,24 @@ isWindowIsAboveCaptureRegion (HWND hwndWindow,
 
 static BOOL CALLBACK
 doCaptureMagnificationAPI_MonitorEnumProc (HMONITOR hMonitor,
-                                           HDC hdcMonitor,
-                                           LPRECT lprcMonitor,
-                                           LPARAM dwData)
+                                           HDC      hdcMonitor,
+                                           LPRECT   lprcMonitor,
+                                           LPARAM   dwData)
 {
+  RECT **rectScreens = (RECT**) dwData;
+
   if (!lprcMonitor) return FALSE;
 
-  if (!rectScreens)
-    rectScreens = (RECT*)malloc(sizeof(RECT)*(rectScreensCount+1));
+  if (! (*rectScreens))
+    *rectScreens = (RECT*) g_malloc (sizeof (RECT)*(rectScreensCount+1));
   else
-    rectScreens = (RECT*)realloc(rectScreens,sizeof(RECT)*(rectScreensCount+1));
+    *rectScreens = (RECT*) g_realloc (rectScreens,
+                                      sizeof (RECT)*(rectScreensCount+1));
 
-  if (rectScreens == NULL) return FALSE;
+  if (*rectScreens == NULL)
+    return FALSE;
 
-  rectScreens[rectScreensCount] = *lprcMonitor;
+  (*rectScreens)[rectScreensCount] = *lprcMonitor;
 
   rectScreensCount++;
 
@@ -673,54 +676,56 @@ doCaptureMagnificationAPI (HWND selectedHwnd,
   /* If the window is maximized then we need to fix the rect variable */
   ZeroMemory (&windowplacment, sizeof (WINDOWPLACEMENT));
   if (GetWindowPlacement (selectedHwnd, &windowplacment) && windowplacment.showCmd == SW_SHOWMAXIMIZED)
-  {
-    /* if this is not the first time we call this function for some reason then we reset the rectScreens 
array */
-    if (rectScreensCount)
     {
-      free (rectScreens);
-      rectScreens = NULL;
-      rectScreensCount = 0;
-    }
-
-    /* Get the screens rects */
-    EnumDisplayMonitors (NULL, NULL, doCaptureMagnificationAPI_MonitorEnumProc, 0);
+      RECT *rectScreens = NULL;
 
+      /* if this is not the first time we call this function for some
+       * reason then we reset the rectScreens count
+       */
+      if (rectScreensCount)
+        rectScreensCount = 0;
 
-    /* If for some reason the array size is 0 then we fill it with the desktop rect */
-    if (!rectScreensCount)
-      {
-        rectScreens = (RECT*)malloc (sizeof(RECT));
-        if (!GetWindowRect (GetDesktopWindow (),rectScreens))
-          /* error: could not get rect screens */
-          return FALSE;
+      /* Get the screens rects */
+      EnumDisplayMonitors (NULL, NULL, doCaptureMagnificationAPI_MonitorEnumProc,
+                           (LPARAM) &rectScreens);
 
-        rectScreensCount = 1;
-      }
+      /* If for some reason the array size is 0 then we fill it with the desktop rect */
+      if (! rectScreensCount)
+        {
+          rectScreens = (RECT*) g_malloc (sizeof (RECT));
+          if (! GetWindowRect (GetDesktopWindow (), rectScreens))
+            {
+              /* error: could not get rect screens */
+              g_free (rectScreens);
+              return FALSE;
+            }
 
-    xCenter = rect.left + (rect.right - rect.left) / 2;
-    yCenter = rect.top + (rect.bottom - rect.top) / 2;
+          rectScreensCount = 1;
+        }
 
-    /* find on which screen the window exist */
-    for (i = 0; i < rectScreensCount; i++)
-      if (xCenter > rectScreens[i].left && xCenter < rectScreens[i].right &&
-          yCenter > rectScreens[i].top && yCenter < rectScreens[i].bottom)
-            break;
+      xCenter = rect.left + (rect.right - rect.left) / 2;
+      yCenter = rect.top + (rect.bottom - rect.top) / 2;
 
-    if (i == rectScreensCount)
-      /* Error: did not found on which screen the window exist */
-      return FALSE;
+      /* find on which screen the window exist */
+      for (i = 0; i < rectScreensCount; i++)
+        if (xCenter > rectScreens[i].left && xCenter < rectScreens[i].right &&
+            yCenter > rectScreens[i].top && yCenter < rectScreens[i].bottom)
+          break;
 
-    if (rectScreens[i].left > rect.left) rect.left = rectScreens[i].left;
-    if (rectScreens[i].right < rect.right) rect.right = rectScreens[i].right;
-    if (rectScreens[i].top > rect.top) rect.top = rectScreens[i].top;
-    if (rectScreens[i].bottom < rect.bottom) rect.bottom = rectScreens[i].bottom;
+      if (i == rectScreensCount)
+        /* Error: did not found on which screen the window exist */
+        return FALSE;
 
-  }
+      if (rectScreens[i].left > rect.left) rect.left = rectScreens[i].left;
+      if (rectScreens[i].right < rect.right) rect.right = rectScreens[i].right;
+      if (rectScreens[i].top > rect.top) rect.top = rectScreens[i].top;
+      if (rectScreens[i].bottom < rect.bottom) rect.bottom = rectScreens[i].bottom;
 
+      g_free (rectScreens);
+    }
 
   rect.right = rect.left + ROUND4(rect.right - rect.left);
 
-
   /* Create the host window that will store the mag child window */
   hwndHost = CreateWindowEx (0x08000000 | 0x080000 | 0x80 | 0x20, APP_NAME, NULL, 0x80000000,
                              0, 0, 0, 0, NULL, NULL, GetModuleHandle (NULL), NULL);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]