Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-02-2011, 03:58 AM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 135
thephotographer is on a distinguished road
Default can you spot the memory leak?

Hi,

Ive got a memory leak in the following code, and after months iv still havent been able to fix it. ive just been ignoring it so far, but i really need to get it fixed. In the dealloc i have commented out CGImageRelease(scratchable) as if i leave it in the app crashes. comment it out and it doesnt crash, but theres a leak. im not sure if thats the cause or not.

in instruments 2 of the things it says are

Code:
#	Category	Event Type	Timestamp	RefCt	Address	Size	Responsible Library	Responsible Caller
0	CFData	Malloc	01:43.595.727	1	0x11d190	32	EvokkeMagazinev0.55	-[myclassLayer initWithFrame:lineWidth:imageLocation:]
1	CFData	CFRetain	01:43.599.675	2	0x11d190	0	CoreGraphics	CGDataProviderCreateWithCFData

any help solving this would be great! thanks

Code:
- (id)initWithFrame:(CGRect)frame lineWidth:(float)lineWidth imageLocation:(NSString*)imageLocation
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        self.transform = CGAffineTransformMakeScale(1, -1);         
        UIImage *scratchableImage = [[UIImage alloc] initWithContentsOfFile:imageLocation];
        if (scratchableImage.size.width != self.frame.size.width)
            scratchable = [self resizeImage:scratchableImage width:self.frame.size.width height:self.frame.size.height].CGImage;
        else
            scratchable = scratchableImage.CGImage;
        width = self.frame.size.width;
		height = self.frame.size.height;
		self.opaque = NO;
		CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
		
		CFMutableDataRef pixels = CFDataCreateMutable( NULL , width * height );
		alphaPixels = CGBitmapContextCreate( CFDataGetMutableBytePtr( pixels ) , width , height , 8 , width , colorspace , kCGImageAlphaNone );
		provider = CGDataProviderCreateWithCFData(pixels);
		
		CGContextSetFillColorWithColor(alphaPixels, [UIColor blackColor].CGColor);
		CGContextFillRect(alphaPixels, frame);
		CGContextSetStrokeColorWithColor(alphaPixels, [UIColor whiteColor].CGColor);
		CGContextSetLineWidth(alphaPixels, lineWidth);
		CGContextSetLineCap(alphaPixels, kCGLineCapRound);
		
		CGImageRef mask = CGImageMaskCreate(width, height, 8, 8, width, provider, nil, NO);
		scratched = CGImageCreateWithMask(scratchable, mask);
		
		CGImageRelease(mask);
		CGColorSpaceRelease(colorspace);
        
        [scratchableImage release];
        
    }
    return self;
}


- (void)dealloc 
{
	CGContextRelease(alphaPixels);
	//CGImageRelease(scratchable);
	CGDataProviderRelease(provider);
    [super dealloc];
}
thephotographer is offline   Reply With Quote
Old 12-02-2011, 04:18 AM   #2 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 28
Mogglas is on a distinguished road
Default

Quote:
Originally Posted by thephotographer View Post
Hi,

Ive got a memory leak in the following code, and after months iv still havent been able to fix it. ive just been ignoring it so far, but i really need to get it fixed. In the dealloc i have commented out CGImageRelease(scratchable) as if i leave it in the app crashes. comment it out and it doesnt crash, but theres a leak. im not sure if thats the cause or not.

in instruments 2 of the things it says are

Code:
#	Category	Event Type	Timestamp	RefCt	Address	Size	Responsible Library	Responsible Caller
0	CFData	Malloc	01:43.595.727	1	0x11d190	32	EvokkeMagazinev0.55	-[myclassLayer initWithFrame:lineWidth:imageLocation:]
1	CFData	CFRetain	01:43.599.675	2	0x11d190	0	CoreGraphics	CGDataProviderCreateWithCFData

any help solving this would be great! thanks

Code:
- (id)initWithFrame:(CGRect)frame lineWidth:(float)lineWidth imageLocation:(NSString*)imageLocation
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        self.transform = CGAffineTransformMakeScale(1, -1);         
        UIImage *scratchableImage = [[UIImage alloc] initWithContentsOfFile:imageLocation];
        if (scratchableImage.size.width != self.frame.size.width)
            scratchable = [self resizeImage:scratchableImage width:self.frame.size.width height:self.frame.size.height].CGImage;
        else
            scratchable = scratchableImage.CGImage;
        width = self.frame.size.width;
		height = self.frame.size.height;
		self.opaque = NO;
		CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
		
		CFMutableDataRef pixels = CFDataCreateMutable( NULL , width * height );
		alphaPixels = CGBitmapContextCreate( CFDataGetMutableBytePtr( pixels ) , width , height , 8 , width , colorspace , kCGImageAlphaNone );
		provider = CGDataProviderCreateWithCFData(pixels);
		
		CGContextSetFillColorWithColor(alphaPixels, [UIColor blackColor].CGColor);
		CGContextFillRect(alphaPixels, frame);
		CGContextSetStrokeColorWithColor(alphaPixels, [UIColor whiteColor].CGColor);
		CGContextSetLineWidth(alphaPixels, lineWidth);
		CGContextSetLineCap(alphaPixels, kCGLineCapRound);
		
		CGImageRef mask = CGImageMaskCreate(width, height, 8, 8, width, provider, nil, NO);
		scratched = CGImageCreateWithMask(scratchable, mask);
		
		CGImageRelease(mask);
		CGColorSpaceRelease(colorspace);
        
        [scratchableImage release];
        
    }
    return self;
}


- (void)dealloc 
{
	CGContextRelease(alphaPixels);
	//CGImageRelease(scratchable);
	CGDataProviderRelease(provider);
    [super dealloc];
}
could it be the 'pixels' variable?
__________________
Games i've developed for iPhone so far:
Skycat and the StarchildrenRushing NinjasChicken Race
Mogglas is offline   Reply With Quote
Old 12-02-2011, 06:54 AM   #3 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 135
thephotographer is on a distinguished road
Default

its quite possible thats the issue.. i tried putting CFRelease(pixels); at the end of the init method, didnt fix the issue. but also im not sure ive done it right, im not sure if CFRelease is the right thing to use, but its the best i could find. theres still a leak.
thephotographer is offline   Reply With Quote
Old 12-02-2011, 09:03 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 28
Mogglas is on a distinguished road
Default

Quote:
Originally Posted by thephotographer View Post
its quite possible thats the issue.. i tried putting CFRelease(pixels); at the end of the init method, didnt fix the issue. but also im not sure ive done it right, im not sure if CFRelease is the right thing to use, but its the best i could find. theres still a leak.
Well, then my tip is: comment out as much as possible. make an empty init, test it. And then uncomment one or a few lines per run. That will make it easy to isolate the leak.
__________________
Games i've developed for iPhone so far:
Skycat and the StarchildrenRushing NinjasChicken Race
Mogglas is offline   Reply With Quote
Old 12-02-2011, 06:30 PM   #5 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 135
thephotographer is on a distinguished road
Default

Quote:
Originally Posted by Mogglas View Post
Well, then my tip is: comment out as much as possible. make an empty init, test it. And then uncomment one or a few lines per run. That will make it easy to isolate the leak.
perfect! i should have thought of that... i could comment out pixels as everything refers to it. but i commented out most of the rest of the code, turned pout to be the variable scratched. i then added CGImageRelease(scratched); to my dealloc and its all good now. thanks! i think that was the last memory leak in my app. i cant find any others
thephotographer is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 390
15 members and 375 guests
AppsBlogger, Clouds, David-T, dedeys78, Duncan C, e2applets, EvilElf, heshiming, iekei, leostc, LunarMoon, Murphy, sacha1996, Sami Gh, teebee74
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,913
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:41 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0