Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 06-02-2009, 03:59 AM   #1 (permalink)
Alx
New Member
 
Join Date: Jan 2009
Posts: 11
Lightbulb Not able to release UIViewController memory

Hi all,

I'm trying to release memory used by a UIViewController containing a UIImageView
but that doesn't work on device.

That seems to work perfectly on Simulator, not on iPhone.

I'm NOT using [UIImage imageNamed] to avoid using the cache of the iPhone.
I'm using a NSMutableDictionary to store all UIImage (loaded with imageWithContentsOfFile ) used in my code:

Code:
NSMutableDictionary *thumbnailCache;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
(...)
thumbnailCache = [[NSMutableDictionary alloc] init];
(...)
}

+ (UIImage*)thumbnailImage:(NSString*)fileName
{
	UIImage *thumbnail = [thumbnailCache objectForKey:fileName];
	
	if (nil == thumbnail)
	{
		thumbnail = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:@"png"]];
		[thumbnailCache setObject:thumbnail forKey:fileName];
		NSLog(@"add image:%@",fileName);
	}
	return thumbnail;
}

+ (void) removeCachedImages
{
	NSLog(@"removeCachedImages:%d",[thumbnailCache count]);
	[thumbnailCache removeAllObjects];
}
When initializing my UIViewController, I create the UIImageView and init it:

Code:
- (id) init {
	self = [super init];
	m_contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	self.view=m_contentView;
	[m_contentView release];
	
	for (int i=0;i<NB_BKG;i++)
	{
		m_bkg[i]=[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 480.0, 320.0)];
		[m_contentView addSubview:m_bkg[i]];
		[m_bkg[i] setImage:[TestViewCtrlAppDelegate thumbnailImage:[NSString stringWithFormat:@"test%02d",i]]];
	}
	
	return self;
}
and here is the code to release my view :

Code:
- (void)dealloc {
	
	for (int i=0;i<NB_BKG;i++)
		[m_bkg[i] release];
	
	[TestViewCtrlAppDelegate removeCachedImages];
    [super dealloc];
}
I can't understand where I missed something.

When running the project in Simulator with performance tool (object allocation) memory is well released.
But on device, memory is not released at all.

Please help me.

Thanks in advance.

Best regards,
Alx
Alx is offline   Reply With Quote
Old 06-02-2009, 11:54 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,577
Default

addSubview also increased the retain count of the subview. Until you remove the subviews from m_contentView OR m_contentView is destroyed, the subviews will not be released by the superview.

I would also move your [m_bkg[i] release] up futher in the code, closer to where you init the imageviews; then you don't have to release them later, just get them removed from the superview or release the superview.

This is how you can add the subviews and release them right away:

Code:
	for (int i=0;i<NB_BKG;i++)
	{
		m_bkg[i]=[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 480.0, 320.0)];
		[m_contentView addSubview:m_bkg[i]];
		[m_bkg[i] release];
		[m_bkg[i] setImage:[TestViewCtrlAppDelegate thumbnailImage:[NSString stringWithFormat:@"test%02d",i]]];

	}

In fact, I'm not sure you need the m_bkg array at all, if its only purpose was to release the imageviews. Maybe it has another purpose?
__________________
smasher 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


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,353
Threads: 39,148
Posts: 171,625
Top Poster: smasher (2,577)
Welcome to our newest member, pyrrho
Powered by vBadvanced CMPS v3.1.0

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