Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Mac Me Up

macrumors regular
Original poster
Jun 25, 2005
170
0
Australia
If I have something like a UILabel linked to a xib file, do I need to release it on dealloc? The reason I ask is because I don't alloc it, which makes me think I don't need to release it either?
eg (in the header):
Code:
IBOutlet UILabel *lblExample;

in the implementation:
Code:
....
[lblExample setText:@"whatever"];
....

-(void)dealloc{
    [lblExample release];//?????????
}
 

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
The reason I ask is because I don't alloc it, which makes me think I don't need to release it either?

Yep, you've got that right - you should not release it unless you alloc/init, retained, or copied the object yourself.

The following is a fairly good article, and there are others out there that I can't seem to find. Even if you think you have a basic grasp of it, reading this can't hurt.

http://www.stepwise.com/Articles/Technical/MemoryManagement.html
 

Mac Me Up

macrumors regular
Original poster
Jun 25, 2005
170
0
Australia
The article you posted doesn't mention anything about interface builder or how that all works, but I understand what you're saying, because the IB magic initialises the label and presumably allocs it, it should release it too.

Just out of interest, can you over release an object (eg: release it more times than it was retained)? And if so what happens?
 

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
The article you posted doesn't mention anything about interface builder or how that all works, but I understand what you're saying, because the IB magic initialises the label and presumably allocs it, it should release it too.

Just out of interest, can you over release an object (eg: release it more times than it was retained)? And if so what happens?

No, the article doesn't mention Interface Builder, but that shouldn't be important once you know basic memory management principles. If you didn't create or retain the object, you don't have ownership of the object and thus should not release it. If you did retain or create it, you took ownership of the object and thus should release it to prevent memory leaks. You should still read the article.

And yes, you can "over-release". Or rather more correctly, if you release an object such that it is deallocated (retain count is reduced to 0) and then send a message to that object (any message, including retain or release), you will get an EXC_BAD_ACCESS crash.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Somewhere deep in the jungles of the iPhone documentation there is an article/guide on how to manage memory management for nib files. You should check it out.
 

Mac Me Up

macrumors regular
Original poster
Jun 25, 2005
170
0
Australia
@kainjow so what you're telling me is that somewhere there is a document I should read? Since you've obviously read it, how about telling me whether the above answer is correct or not? Even better how about a name...or location of this document?

The funny thing I posted the same question to a different apple forum, and got the opposite response, eg: that I should release them because the act of creating them in IB is the same as allocating them...so now I'm confused :confused:
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
I didn't know off hand the name, but I found it now. In the iPhone docs:
Guides > Resource Programming Guide > Nib Files > The Nib Object Life Cycle > Nib Object Retention
 

Mac Me Up

macrumors regular
Original poster
Jun 25, 2005
170
0
Australia
Ahhh legendary, thanks!

@Sbrocket: you might want to have a read of that article as well. In short basically it says (from what I can gather) that for iPhone development your answer is wrong, you do need to retain and release IB objects, because they can be released prematurely if you don't.
 

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
@Sbrocket: you might want to have a read of that article as well. In short basically it says (from what I can gather) that for iPhone development your answer is wrong, you do need to retain and release IB objects, because they can be released prematurely if you don't.

Nowhere did I say that you shouldn't retain it, just that you definitely shouldn't release it unless you retain it first. That still holds true. :p

I don't usually use Interface Builder to build anything beyond templates (and infrequently at that) because I find it clunky and half-baked. That's not to say it isn't the right tool for others, just that I wouldn't presume to know everything about its use.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
@Mac Me Up

The section on retaining nib objects begins with "Each time you ask the NSBundle or NSNib class to load a nib file" and then mentions that the top level objects need to be retained. You can assume that UIViewController makes those calls and does the appropriate retain/release of the top level objects. Unless you're explicitly using the NSBundle api for loading nibs you don't need to do anything special.
 

fenrus110

macrumors regular
Mar 24, 2008
142
0
Some of the Apple's Sample Code do release IBOutlet objects without having alloc, retain, etc. anywhere in the code. PageControl is one example, but I'm sure I've seen others.

I've released IBOutlet's in the' dealloc' and it doesn't seem to have any negative affect on my code, nor can I really confirm if it has a positive affect either.
 

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
Some of the Apple's Sample Code do release IBOutlet objects without having alloc, retain, etc. anywhere in the code.

That's incorrect. PageControl, for example, has a synthesized retain setter for the UILabel "pageNumberLabel" in its MyViewController class, and then the corresponding release statement is in dealloc. The same is true for the objects in the AppDelegate class. If you didn't release these objects in dealloc, you would have a memory leak as you took ownership of the object (in the setter) and never relinquished ownership.

This is the correct way to handle it - whenever you have a synthesized retain setter, you need to release the object in dealloc to be "correct" memory management-wise. For objects that aren't deallocated until the application quits it doesn't really matter since the memory is reclaimed either way, but its good to get in the habit so that you don't end up with leaks for objects that are created and released continually.
 

mac-dev-7

macrumors newbie
Nov 12, 2009
4
0
UILabel memory leak

Hi all,

I'm relatively new to the iphone development realm, so any help is greatly appreciated.

I am getting a memory leak on a UILabel text field and I can't figure out where the leak is coming from, since I believe I'm releasing the required data in the dealloc method. When I comment out the code line that sets the label text, I see no memory leaks through Instruments. With the line in place I'm getting GeneralBlock-16 leaks every time the label text is reset to a new value. The label is part of a simple view that displays images and captions underneath them, with a previous and next button in place -- each button loads a new image and new caption (the label). The code I'm using for the label load is:

In .h file:

Code:
IBOutlet UILabel *caption;
...

@property (nonatomic, retain) UILabel *caption;

In .m file: (controller)

Code:
@synthesize caption;

...
[self.caption setText: @"some caption text"];

//the above text for the caption is actually grabbed from another helper
method that loads the data asynch., but even with the above line of code
throughout I get one GeneralBlock-16 leak --i.e. i'm assuming one since 
the string isn't changing its value, which doesn't make sense at all to me.
...

- (void)dealloc {
  ...
 	self.caption.text = nil;
	self.caption = nil;
	[self.caption release];
  ...

       [super dealloc];
}

//i've tried the nil on all of the above, removing the nil with just a release
etc. with no change in Instruments...still the same generalblock leak.

I've spent about 3 days on this and am stuck. If anyone has encountered the same issue, please let me know.

Thanks!
 

Luke Redpath

macrumors 6502a
Nov 9, 2007
733
6
Colchester, UK
Here's your problem:

You now are calling release on nil. caption is leaked because you wiped out the pointer before releasing its memory.

Actually I'm pretty sure the synthesised setter will do the release for you when you set the property to nil, making the second release pointless, but not a cause of a leak.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
mac-dev-7, All the code you show seems correct, although not idiomatic.

Are you setting the text on the label from a background thread? Don't do that.
 

mac-dev-7

macrumors newbie
Nov 12, 2009
4
0
Why not a background thread? Is that a potential for this type of leak? What do you recommend using?

Thanks.
 

mac-dev-7

macrumors newbie
Nov 12, 2009
4
0
I'm using the following code:
Code:
- (void) loadImage:(Image *) img{
	[self.operationQueue cancelAllOperations];
	[self.view bringSubviewToFront:self.spinnerWheel];
	[self.spinnerWheel startAnimating];
	NSInvocationOperation *imageLoadOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(synchronousImageLoad:) object:img];
	[self.operationQueue addOperation:imageLoadOperation];
	[imageLoadOperation release];
}

- (void) synchronousImageLoad:(Image *)img {
	self.imageView.image = img.data;
	[self.spinnerWheel stopAnimating];
	[self.caption setText: img.caption];
}
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
UIKit isn't thread safe and you can't access UIKit methods from anything other than the main thread.

Use performSelectorOnMainThread to set the label from a background thread. Something like this:

Code:
[viewController performSelectorOnMainThread:@selector(updateLabel:) withObject:myString waitUntilDone:NO];

// ...

-(void)updateLabel:(NSString*)inString
{
     [self.caption setText: inString];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.