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 04-16-2009, 12:23 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 94
yezaev is on a distinguished road
Default Subviews and dealloc - am I an idiot?

I've been coding for the iphone for nearly 9 months now, and I've always been under the impression that when a UIView instance is deallocated, each of its subviews is removed from it and sent the release message.
Something's come up and I'm wondering if I just dreamed that up.
Does this actually happen?
yezaev is offline   Reply With Quote
Old 05-05-2009, 03:03 PM   #2 (permalink)
BdR
Registered Member
 
BdR's Avatar
 
Join Date: Mar 2009
Location: The Netherlands
Posts: 132
BdR is on a distinguished road
Default Re: Subviews and dealloc - am I an idiot?

Good question, I wanted to ask the same thing. I guess there's no way to verify subviews being deallocated, except to watch Instruments app and see if memory use keeps rising..? Isn't there a better way to monitor this?

Anyway, here's my code example..
Code:
@interface MyTile : UIView {
	NSInteger theNumber;
}
@property (assign, nonatomic) NSInteger theNumber;

-(id)initWithFrame:(CGRect)frame andNumber:(int)aNumber;
and the code to initialise MyTile view
Code:
-(id)initWithFrame:(CGRect)frame andNumber:(int)aNumber {
	[super initWithFrame: frame];

	// load the image and resize it
	UIImageView *tmpImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tilegraphic.png"]];
	tmpImage.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);

	// add image to view
	[self addSubview: tmpImage];
	[tmpImage release];

	// set number property
	self.theNumber = aNumber;
	
	// add label with text
	UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
	tmpLabel.backgroundColor = [UIColor clearColor];
	[tmpLabel setTextAlignment:UITextAlignmentCenter];
	tmpLabel.text = @"123";
	
	// add labelview to imageview
	[self addSubview:tmpLabel];
	[tmpLabel release];

	// return pointer to self, for use outside this constructor method
	return self;
}
Now, when I'm done with the tiles from one game (each new game can have different tile dimensions etc.) I send a release to all of the MyTiles.

The question is: will the UIImageView ("tilegraphics.png") and the UILabel ("123") views also be released or will they cause a memory leak? If so, do I have to add a UIImageView and UILabel pointer to MyView class, just so I can send them a release-message in the MyTile dealloc?
BdR is offline   Reply With Quote
Reply

Bookmarks

Tags
dealloc, memory management, uiview

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: 323
7 members and 316 guests
blueorb, guusleijsten, jbro, Kryckter, mer10, n00b, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

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