From 98a4e492040f9c5284e8f39276f1d559f6d5adce Mon Sep 17 00:00:00 2001 From: ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com> Date: Mon, 2 Nov 2020 13:13:41 +0100 Subject: Fix possible memory leak in Fl_WinAPI_Screen_Driver::read_win_rectangle_unscaled() Thanks to "fire-eggs" for spotting it. Also minor optimisations in Fl_X11_Screen_Driver::read_win_rectangle_unscaled() and Fl_Cocoa_Screen_Driver::read_win_rectangle_unscaled(). This closes PR #151. --- src/drivers/WinAPI/Fl_WinAPI_Screen_Driver.cxx | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) (limited to 'src/drivers/WinAPI') diff --git a/src/drivers/WinAPI/Fl_WinAPI_Screen_Driver.cxx b/src/drivers/WinAPI/Fl_WinAPI_Screen_Driver.cxx index f65404922..873e76a4c 100644 --- a/src/drivers/WinAPI/Fl_WinAPI_Screen_Driver.cxx +++ b/src/drivers/WinAPI/Fl_WinAPI_Screen_Driver.cxx @@ -503,15 +503,8 @@ Fl_WinAPI_Screen_Driver::read_win_rectangle( Fl_RGB_Image *Fl_WinAPI_Screen_Driver::read_win_rectangle_unscaled(int X, int Y, int w, int h, Fl_Window *win) { - int d = 3; // Depth of image - int alpha = 0; uchar *p = NULL; - // Allocate the image data array as needed... - const uchar *oldp = p; - if (!p) p = new uchar[w * h * d]; - - // Initialize the default colors/alpha in the whole image... - memset(p, alpha, w * h * d); - + // Depth of image is always 3 here + // Grab all of the pixels in the image... // Assure that we are not trying to read non-existing data. If it is so, the @@ -534,7 +527,12 @@ Fl_RGB_Image *Fl_WinAPI_Screen_Driver::read_win_rectangle_unscaled(int X, int Y, Y = 0; } - if (h < 1 || w < 1) return 0/*p*/; // nothing to copy + if (h < 1 || w < 1) return 0; // nothing to copy + + // Allocate and initialize the image data array + size_t arraySize = ((size_t)w * h) * 3; + uchar *p = new uchar[arraySize]; + memset(p, 0, arraySize); int line_size = ((3*w+3)/4) * 4; // each line is aligned on a DWORD (4 bytes) uchar *dib = new uchar[line_size*h]; // create temporary buffer to read DIB @@ -572,15 +570,13 @@ Fl_RGB_Image *Fl_WinAPI_Screen_Driver::read_win_rectangle_unscaled(int X, int Y, for (int j = 0; jalloc_array = 1; + Fl_RGB_Image *rgb = new Fl_RGB_Image(p, w, h, 3); + rgb->alloc_array = 1; return rgb; } -- cgit v1.2.3