Fix memory leak in set_custom_mouse_cursor

This commit is contained in:
Guilherme Felipe 2018-05-26 15:54:38 -03:00
parent 2f56eb9bb8
commit dda8937ef3
3 changed files with 17 additions and 5 deletions

View File

@ -1226,7 +1226,7 @@ void OS_OSX::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, c
Image image = texture->get_data(); Image image = texture->get_data();
NSBitmapImageRep *imgrep = [[[NSBitmapImageRep alloc] NSBitmapImageRep *imgrep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL initWithBitmapDataPlanes:NULL
pixelsWide:int(texture_size.width) pixelsWide:int(texture_size.width)
pixelsHigh:int(texture_size.height) pixelsHigh:int(texture_size.height)
@ -1236,7 +1236,7 @@ void OS_OSX::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, c
isPlanar:NO isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace colorSpaceName:NSDeviceRGBColorSpace
bytesPerRow:int(texture_size.width) * 4 bytesPerRow:int(texture_size.width) * 4
bitsPerPixel:32] autorelease]; bitsPerPixel:32];
ERR_FAIL_COND(imgrep == nil); ERR_FAIL_COND(imgrep == nil);
uint8_t *pixels = [imgrep bitmapData]; uint8_t *pixels = [imgrep bitmapData];
@ -1263,9 +1263,10 @@ void OS_OSX::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, c
pixels[i * 4 + 3] = alpha; pixels[i * 4 + 3] = alpha;
} }
NSImage *nsimage = [[[NSImage alloc] initWithSize:NSMakeSize(texture_size.width, texture_size.height)] autorelease]; NSImage *nsimage = [[NSImage alloc] initWithSize:NSMakeSize(texture_size.width, texture_size.height)];
[nsimage addRepresentation:imgrep]; [nsimage addRepresentation:imgrep];
[cursors[p_shape] release];
NSCursor *cursor = [[NSCursor alloc] initWithImage:nsimage hotSpot:NSMakePoint(p_hotspot.x, p_hotspot.y)]; NSCursor *cursor = [[NSCursor alloc] initWithImage:nsimage hotSpot:NSMakePoint(p_hotspot.x, p_hotspot.y)];
cursors[p_shape] = cursor; cursors[p_shape] = cursor;
@ -1273,6 +1274,9 @@ void OS_OSX::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, c
if (p_shape == CURSOR_ARROW) { if (p_shape == CURSOR_ARROW) {
[cursor set]; [cursor set];
} }
[imgrep release];
[nsimage release];
} }
} }

View File

@ -2017,7 +2017,7 @@ void OS_Windows::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shap
UINT size = sizeof(UINT) * image_size; UINT size = sizeof(UINT) * image_size;
// Create the BITMAP with alpha channel // Create the BITMAP with alpha channel
COLORREF *buffer = (COLORREF *)malloc(sizeof(COLORREF) * image_size); COLORREF *buffer = (COLORREF *)memalloc(sizeof(COLORREF) * image_size);
for (UINT index = 0; index < image_size; index++) { for (UINT index = 0; index < image_size; index++) {
int row_index = floor(index / texture_size.width) + atlas_rect.pos.y; int row_index = floor(index / texture_size.width) + atlas_rect.pos.y;
@ -2042,6 +2042,8 @@ void OS_Windows::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shap
GetMaskBitmaps(bitmap, clrTransparent, hAndMask, hXorMask); GetMaskBitmaps(bitmap, clrTransparent, hAndMask, hXorMask);
if (NULL == hAndMask || NULL == hXorMask) { if (NULL == hAndMask || NULL == hXorMask) {
memfree(buffer);
DeleteObject(bitmap);
return; return;
} }
@ -2066,6 +2068,9 @@ void OS_Windows::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shap
if (hXorMask != NULL) { if (hXorMask != NULL) {
DeleteObject(hXorMask); DeleteObject(hXorMask);
} }
memfree(buffer);
DeleteObject(bitmap);
} }
} }

View File

@ -2198,7 +2198,7 @@ void OS_X11::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, c
cursor_image->yhot = p_hotspot.y; cursor_image->yhot = p_hotspot.y;
// allocate memory to contain the whole file // allocate memory to contain the whole file
cursor_image->pixels = (XcursorPixel *)malloc(size); cursor_image->pixels = (XcursorPixel *)memalloc(size);
for (XcursorPixel index = 0; index < image_size; index++) { for (XcursorPixel index = 0; index < image_size; index++) {
int row_index = floor(index / texture_size.width) + atlas_rect.pos.y; int row_index = floor(index / texture_size.width) + atlas_rect.pos.y;
@ -2220,6 +2220,9 @@ void OS_X11::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, c
if (p_shape == CURSOR_ARROW) { if (p_shape == CURSOR_ARROW) {
XDefineCursor(x11_display, x11_window, cursors[p_shape]); XDefineCursor(x11_display, x11_window, cursors[p_shape]);
} }
memfree(cursor_image->pixels);
XcursorImageDestroy(cursor_image);
} }
} }