From 857608495c54e94b641dc01486fcfb36860aad82 Mon Sep 17 00:00:00 2001 From: Donald Alan Morrison Date: Fri, 24 Aug 2012 23:34:20 -0700 Subject: [PATCH] Plugged CoreFoundation memory leaks identified by Static Analyzer. --- cocos2dx/platform/mac/CCImage.mm | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/cocos2dx/platform/mac/CCImage.mm b/cocos2dx/platform/mac/CCImage.mm index 0e38868ff5..4043bd1f1a 100755 --- a/cocos2dx/platform/mac/CCImage.mm +++ b/cocos2dx/platform/mac/CCImage.mm @@ -235,7 +235,7 @@ static bool _initPremultipliedATextureWithImage(CGImageRef image, NSUInteger POT CGContextRelease(context); return true; } - +// TODO: rename _initWithImage, it also makes a draw call. static bool _initWithImage(CGImageRef CGImage, tImageInfo *pImageinfo, double scaleX, double scaleY) { NSUInteger POTWide, POTHigh; @@ -258,10 +258,8 @@ static bool _initWithImage(CGImageRef CGImage, tImageInfo *pImageinfo, double sc } - // always load premultiplied images - _initPremultipliedATextureWithImage(CGImage, POTWide, POTHigh, pImageinfo); - - return true; + // load and draw image + return _initPremultipliedATextureWithImage(CGImage, POTWide, POTHigh, pImageinfo); } static bool _initWithFile(const char* path, tImageInfo *pImageinfo) @@ -277,17 +275,19 @@ static bool _initWithFile(const char* path, tImageInfo *pImageinfo) jpg = [[NSImage alloc] initWithContentsOfFile: fullPath]; //png = [[NSImage alloc] initWithData:UIImagePNGRepresentation(jpg)]; CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)[jpg TIFFRepresentation], NULL); - CGImage = CGImageSourceCreateImageAtIndex(source, 0, NULL); + CGImage = CGImageSourceCreateImageAtIndex(source, 0, NULL); ret = _initWithImage(CGImage, pImageinfo, 1.0, 1.0); //[png release]; [jpg release]; - + if (CGImage) CFRelease(CGImage); + if (source) CFRelease(source); + return ret; } - +// TODO: rename _initWithData, it also makes a draw call. static bool _initWithData(void * pBuffer, int length, tImageInfo *pImageinfo, double scaleX, double scaleY) { bool ret = false; @@ -302,8 +302,9 @@ static bool _initWithData(void * pBuffer, int length, tImageInfo *pImageinfo, do CGImage = CGImageSourceCreateImageAtIndex(source, 0, NULL); ret = _initWithImage(CGImage, pImageinfo, scaleX, scaleY); + if (CGImage) CFRelease(CGImage); + if (source) CFRelease(source); } - return ret; }