From 0b37d23c37f8475930af60bc9e6244b74d582ed2 Mon Sep 17 00:00:00 2001 From: David Capello Date: Tue, 10 Jan 2017 11:10:06 -0300 Subject: [PATCH] Fix memory leaks changing custom native cursors on macOS --- src/she/osx/window.mm | 54 ++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/she/osx/window.mm b/src/she/osx/window.mm index 92de44367..aa2a11205 100644 --- a/src/she/osx/window.mm +++ b/src/she/osx/window.mm @@ -187,37 +187,39 @@ using namespace she; } } - CGDataProviderRef dataRef = - CGDataProviderCreateWithData(nullptr, &buf[0], - w*h*4, nullptr); - if (!dataRef) - return NO; + @autoreleasepool { + CGDataProviderRef dataRef = + CGDataProviderCreateWithData(nullptr, &buf[0], + w*h*4, nullptr); + if (!dataRef) + return NO; - CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); - CGImageRef img = - CGImageCreate( - w, h, 8, 32, 4*w, - colorSpace, kCGImageAlphaLast, dataRef, - nullptr, false, kCGRenderingIntentDefault); - CGColorSpaceRelease(colorSpace); + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); + CGImageRef img = + CGImageCreate( + w, h, 8, 32, 4*w, + colorSpace, kCGImageAlphaLast, dataRef, + nullptr, false, kCGRenderingIntentDefault); + CGColorSpaceRelease(colorSpace); - CGDataProviderRelease(dataRef); + CGDataProviderRelease(dataRef); - NSCursor* nsCursor = nullptr; - if (img) { - NSImage* image = - [[NSImage new] initWithCGImage:img - size:NSMakeSize(w, h)]; - CGImageRelease(img); + NSCursor* nsCursor = nullptr; + if (img) { + NSImage* image = + [[NSImage alloc] initWithCGImage:img + size:NSMakeSize(w, h)]; + CGImageRelease(img); - nsCursor = - [[NSCursor new] initWithImage:image - hotSpot:NSMakePoint(scale*focus.x + scale/2, - scale*focus.y + scale/2)]; + nsCursor = + [[NSCursor alloc] initWithImage:image + hotSpot:NSMakePoint(scale*focus.x + scale/2, + scale*focus.y + scale/2)]; + } + if (nsCursor) + [self.contentView setCursor:nsCursor]; + return (nsCursor ? YES: NO); } - if (nsCursor) - [self.contentView setCursor:nsCursor]; - return (nsCursor ? YES: NO); } - (void)noResponderFor:(SEL)eventSelector