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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 08-29-2009, 07:47 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 30
Default problem releasing memory - memoryleaks

So I'm making some filters for my iPhone app and everything goes fine as long as I work with small images, but when I bring in the big life size ones like the one the iPhone camera shoots, I'm experiencing some errors probably caused by memoryleaks or resources not released correctly.

So this is my code for one filter :
PHP Code:
- (IBAction)MonoImage {
    
        
//previoushit checks if a filter was already activated and if it is, it restores it and before applying the new filter, if it isn't it saves the new image to an UIimage called "copyOfimage2".

    
if (previousHit == 1)
    {
        
CGImageRef cgImage = [copyOfImage2 CGImage];
        
image.image = [[UIImage allocinitWithCGImage:cgImage];
        
CGImageRelease(cgImage);
        
    }
    else
    {
        
CGImageRef cgImage = [image.image CGImage];
        
copyOfImage2 = [[UIImage allocinitWithCGImage:cgImage];    
        
CGImageRelease(cgImage);
    }
    
    
previousHit 1;
    
    
CGImageRef inImage image.image.CGImage;         
    
CGContextRef ctx
    
    
    
CFDataRef m_DataRef
    
m_DataRef CGDataProviderCopyData(CGImageGetDataProvider(inImage)); 
    
UInt8 m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef); 
    
Byte tmpByte
    
int length CFDataGetLength(m_DataRef); 

    
    for (
int index 0index lengthindex += 4
    { 
        
tmpByte = (m_PixelBuf[index 1] + m_PixelBuf[index 2] + m_PixelBuf[index 3]) / 3
        if (
tmpByte >= 128
            
m_PixelBuf[index 1] = m_PixelBuf[index 2] = m_PixelBuf[index 3] = 255
        else 
            
m_PixelBuf[index 1] = m_PixelBuf[index 2] = m_PixelBuf[index 3] = 0
        
    } 
    
    
    
    
ctx CGBitmapContextCreate(m_PixelBuf
                                
CGImageGetWidthinImage ), 
                                
CGImageGetHeightinImage ), 
                                
8
                                
CGImageGetBytesPerRowinImage ), 
                                
CGImageGetColorSpaceinImage ), 
                                
kCGImageAlphaPremultipliedFirst ); 
    
    
    
CGImageRef imageRef CGBitmapContextCreateImage (ctx); 
    
UIImagerawImage = [UIImage imageWithCGImage:imageRef]; 
    
    
CGContextRelease(ctx); 
    
    
image.image rawImage
      
CFRelease(m_DataRef);
    
CGImageRelease(imageRef);

So I presume some CGimageRefs aren't released properly so I'm trying to find out a way to optimize this code to release them.

I also have some of these filters tied to an UISlider, so this can be annoyingly slow in this way and can even cause the app to crash.

Can anyone help?
ykrsdn is offline   Reply With Quote
Old 08-29-2009, 08:31 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

Items in bold are (or will be) leaks. See the properties link in my signature for more info.

Code:
- (IBAction)MonoImage {
	
        //previoushit checks if a filter was already activated and if it is, it restores it and before applying the new filter, if it isn't it saves the new image to an UIimage called "copyOfimage2".

	if (previousHit == 1)
	{
		CGImageRef cgImage = [copyOfImage2 CGImage];
		image.image = [[UIImage alloc] initWithCGImage:cgImage];
		CGImageRelease(cgImage);
		
	}
	else
	{
		CGImageRef cgImage = [image.image CGImage];
		copyOfImage2 = [[UIImage alloc] initWithCGImage:cgImage];	
		CGImageRelease(cgImage);
	}
	
	previousHit = 1;
	
	CGImageRef inImage = image.image.CGImage;         
    CGContextRef ctx; 
	
	
	CFDataRef m_DataRef; 
	m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage)); 
	UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef); 
	Byte tmpByte; 
	int length = CFDataGetLength(m_DataRef); 

	
	for (int index = 0; index < length; index += 4) 
	{ 
		tmpByte = (m_PixelBuf[index + 1] + m_PixelBuf[index + 2] + m_PixelBuf[index + 3]) / 3; 
		if (tmpByte >= 128) 
			m_PixelBuf[index + 1] = m_PixelBuf[index + 2] = m_PixelBuf[index + 3] = 255; 
		else 
			m_PixelBuf[index + 1] = m_PixelBuf[index + 2] = m_PixelBuf[index + 3] = 0; 
		
	} 
	
	
	
	ctx = CGBitmapContextCreate(m_PixelBuf, 
								CGImageGetWidth( inImage ), 
								CGImageGetHeight( inImage ), 
								8, 
								CGImageGetBytesPerRow( inImage ), 
								CGImageGetColorSpace( inImage ), 
								kCGImageAlphaPremultipliedFirst ); 
	
	
	CGImageRef imageRef = CGBitmapContextCreateImage (ctx); 
	UIImage* rawImage = [UIImage imageWithCGImage:imageRef]; 
	
	CGContextRelease(ctx); 
	
    image.image = rawImage; 
  	CFRelease(m_DataRef);
	CGImageRelease(imageRef);
}
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 08-29-2009, 08:46 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 30
Default no clue

Quote:
Originally Posted by BrianSlick View Post
Items in bold are (or will be) leaks. See the properties link in my signature for more info.

Code:
- (IBAction)MonoImage {
	
        //previoushit checks if a filter was already activated and if it is, it restores it and before applying the new filter, if it isn't it saves the new image to an UIimage called "copyOfimage2".

	if (previousHit == 1)
	{
		CGImageRef cgImage = [copyOfImage2 CGImage];
		image.image = [[UIImage alloc] initWithCGImage:cgImage];
		CGImageRelease(cgImage);
		
	}
	else
	{
		CGImageRef cgImage = [image.image CGImage];
		copyOfImage2 = [[UIImage alloc] initWithCGImage:cgImage];	
		CGImageRelease(cgImage);
	}
	
	previousHit = 1;
	
	CGImageRef inImage = image.image.CGImage;         
    CGContextRef ctx; 
	
	
	CFDataRef m_DataRef; 
	m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage)); 
	UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef); 
	Byte tmpByte; 
	int length = CFDataGetLength(m_DataRef); 

	
	for (int index = 0; index < length; index += 4) 
	{ 
		tmpByte = (m_PixelBuf[index + 1] + m_PixelBuf[index + 2] + m_PixelBuf[index + 3]) / 3; 
		if (tmpByte >= 128) 
			m_PixelBuf[index + 1] = m_PixelBuf[index + 2] = m_PixelBuf[index + 3] = 255; 
		else 
			m_PixelBuf[index + 1] = m_PixelBuf[index + 2] = m_PixelBuf[index + 3] = 0; 
		
	} 
	
	
	
	ctx = CGBitmapContextCreate(m_PixelBuf, 
								CGImageGetWidth( inImage ), 
								CGImageGetHeight( inImage ), 
								8, 
								CGImageGetBytesPerRow( inImage ), 
								CGImageGetColorSpace( inImage ), 
								kCGImageAlphaPremultipliedFirst ); 
	
	
	CGImageRef imageRef = CGBitmapContextCreateImage (ctx); 
	UIImage* rawImage = [UIImage imageWithCGImage:imageRef]; 
	
	CGContextRelease(ctx); 
	
    image.image = rawImage; 
  	CFRelease(m_DataRef);
	CGImageRelease(imageRef);
}
I was already thinking in that direction but the problem is that I don't know how to release the Cgimages, hence I'm using "CGImageRelease(cgImage);", don't even know if that's the right method. I checked your signature link, but it hasn't made me any wiser.
ykrsdn is offline   Reply With Quote
Old 08-29-2009, 08:51 PM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

This:

Code:
image.image = [[UIImage alloc] initWithCGImage:cgImage];
Should be more like:

Code:
UIImage *anImage = [[UIImage alloc] initWithCGImage:cgImage];
[[self image] setImage: anImage];
[anImage release], anImage = nil;
No idea about the CG stuff, but that isn't what I highlighted anyway.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 08-29-2009, 08:56 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 30
Default

Quote:
Originally Posted by BrianSlick View Post
This:

Code:
image.image = [[UIImage alloc] initWithCGImage:cgImage];
Should be more like:

Code:
UIImage *anImage = [[UIImage alloc] initWithCGImage:cgImage];
[[self image] setImage: anImage];
[anImage release], anImage = nil;
No idea about the CG stuff, but that isn't what I highlighted anyway.
Ok thanks, I'll give it a shot!
ykrsdn is offline   Reply With Quote
Old 08-29-2009, 09:08 PM   #6 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 30
Default Works fine

I've tried and it works fine, but my images are deformed anyway, so I will give up. Seems an iPhone isn't really made to perform this kind of graphical operations anyway. It has nothing to do with the memory leaks apparently, it are just the images the iPhone's camera generates that are reacting bad to the filters.

Thanks Anyway!
ykrsdn is offline   Reply With Quote
Reply

Bookmarks

Tags
autorelease, cgimage, filters, release, uiimage

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: 265
17 members and 248 guests
ADY, Alsahir, dacapo, Dani77, Desert Diva, djohnson, F_Bryant, Grinarn, HemiMG, jansan, linkmx, M@realobjects, macquitzon216, prchn4christ, smethorst, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

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