juce-framework / JUCE

JUCE is an open-source cross-platform C++ application framework for desktop and mobile applications, including VST, VST3, AU, AUv3, LV2 and AAX audio plug-ins.
https://juce.com
Other
6.49k stars 1.72k forks source link

Memory Leak in MouseCursorHelpers::fromHIServices #110

Closed hjmallon closed 7 years ago

hjmallon commented 7 years ago

Please include:

On startup of JUCE UI I am getting memory leaks reported in Instruments. NSImage in MouseCursorHelpers::fromHIServices. OSX 10.11 64bit. Adding [originalImage release]; as below seems to solve it. Unfortunately I don't have a test program. My knowledge of OSX APIs (thanks to the ease of use of JUCE...) is terrible, so this might be way off.

const String cursorPath (String ("/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/"
                                    "HIServices.framework/Versions/A/Resources/cursors/")
                            + filename);

NSImage* originalImage = [[NSImage alloc] initByReferencingFile: juceStringToNS (cursorPath + "/cursor.pdf")];
NSSize originalSize = [originalImage size];
NSImage* resultImage   = [[NSImage alloc] initWithSize: originalSize];

for (int scale = 1; scale <= 4; ++scale)
{
    NSAffineTransform* scaleTransform = [NSAffineTransform transform];
    [scaleTransform scaleBy: (float) scale];

    if (CGImageRef rasterCGImage = [originalImage CGImageForProposedRect: nil
                                                                    context: nil
                                                                    hints: [NSDictionary dictionaryWithObjectsAndKeys:
                                                                                NSImageHintCTM, scaleTransform, nil]])
    {
        NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithCGImage: rasterCGImage];
        [imageRep setSize: originalSize];

        [resultImage addRepresentation: imageRep];
        [imageRep release];
    }
    else
    {
        return nil;
    }
}

[originalImage release]; // <----

NSDictionary* info = [NSDictionary dictionaryWithContentsOfFile: juceStringToNS (cursorPath + "/info.plist")];

const float hotspotX = (float) [[info valueForKey: nsStringLiteral ("hotx")] doubleValue];
const float hotspotY = (float) [[info valueForKey: nsStringLiteral ("hoty")] doubleValue];

return fromNSImage (resultImage, NSMakePoint (hotspotX, hotspotY));
hogliux commented 7 years ago

Yes thank you!