Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 14, 2008, 09:17 PM   #1
Mac Me Up
macrumors regular
 
Join Date: Jun 2005
Location: Australia
Do I need to release xib resources?

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];//?????????
}
__________________
My iPhone apps: Shifty Jelly & Tennis Stats
Mac Me Up is offline   0 Reply With Quote
Old Sep 15, 2008, 12:00 AM   #2
Sbrocket
macrumors 65816
 
Sbrocket's Avatar
 
Join Date: Jun 2007
Location: /dev/null
Quote:
Originally Posted by Mac Me Up View Post
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/Tec...anagement.html
Sbrocket is offline   0 Reply With Quote
Old Sep 15, 2008, 12:05 AM   #3
Mac Me Up
Thread Starter
macrumors regular
 
Join Date: Jun 2005
Location: 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?
__________________
My iPhone apps: Shifty Jelly & Tennis Stats
Mac Me Up is offline   0 Reply With Quote
Old Sep 15, 2008, 12:13 AM   #4
Sbrocket
macrumors 65816
 
Sbrocket's Avatar
 
Join Date: Jun 2007
Location: /dev/null
Quote:
Originally Posted by Mac Me Up View Post
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.
Sbrocket is offline   0 Reply With Quote
Old Sep 15, 2008, 12:37 AM   #5
Mac Me Up
Thread Starter
macrumors regular
 
Join Date: Jun 2005
Location: Australia
Excellent...thanks for all your help.
__________________
My iPhone apps: Shifty Jelly & Tennis Stats
Mac Me Up is offline   0 Reply With Quote
Old Sep 15, 2008, 02:20 AM   #6
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
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.
kainjow is offline   0 Reply With Quote
Old Sep 15, 2008, 02:26 AM   #7
Mac Me Up
Thread Starter
macrumors regular
 
Join Date: Jun 2005
Location: 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
__________________
My iPhone apps: Shifty Jelly & Tennis Stats
Mac Me Up is offline   0 Reply With Quote
Old Sep 15, 2008, 02:41 AM   #8
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
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
kainjow is offline   0 Reply With Quote
Old Sep 15, 2008, 02:51 AM   #9
Mac Me Up
Thread Starter
macrumors regular
 
Join Date: Jun 2005
Location: 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.
__________________
My iPhone apps: Shifty Jelly & Tennis Stats
Mac Me Up is offline   0 Reply With Quote
Old Sep 15, 2008, 08:40 AM   #10
Sbrocket
macrumors 65816
 
Sbrocket's Avatar
 
Join Date: Jun 2007
Location: /dev/null
Quote:
Originally Posted by Mac Me Up View Post
@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.

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.
Sbrocket is offline   0 Reply With Quote
Old Sep 15, 2008, 09:53 AM   #11
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
@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.
PhoneyDeveloper is offline   0 Reply With Quote
Old Sep 15, 2008, 11:46 AM   #12
fenrus110
macrumors regular
 
Join Date: Mar 2008
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.
fenrus110 is offline   0 Reply With Quote
Old Sep 15, 2008, 02:47 PM   #13
Sbrocket
macrumors 65816
 
Sbrocket's Avatar
 
Join Date: Jun 2007
Location: /dev/null
Quote:
Originally Posted by fenrus110 View Post
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.
Sbrocket is offline   0 Reply With Quote
Old Nov 12, 2009, 12:26 PM   #14
mac-dev-7
macrumors newbie
 
Join Date: Nov 2009
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!
mac-dev-7 is offline   0 Reply With Quote
Old Nov 12, 2009, 12:32 PM   #15
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Here's your problem:
Quote:
Originally Posted by mac-dev-7 View Post
Code:
	self.caption = nil;
	[self.caption release];
You now are calling release on nil. caption is leaked because you wiped out the pointer before releasing its memory.
__________________
dejo is offline   0 Reply With Quote
Old Nov 12, 2009, 12:48 PM   #16
Luke Redpath
macrumors 6502a
 
Join Date: Nov 2007
Quote:
Originally Posted by dejo View Post
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.
Luke Redpath is offline   0 Reply With Quote
Old Nov 12, 2009, 12:56 PM   #17
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by Luke Redpath View Post
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.
Hmm, upon re-examination, I believe you're on to something. D'oh!
__________________
dejo is offline   0 Reply With Quote
Old Nov 12, 2009, 01:01 PM   #18
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
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.
PhoneyDeveloper is offline   0 Reply With Quote
Old Nov 12, 2009, 01:16 PM   #19
mac-dev-7
macrumors newbie
 
Join Date: Nov 2009
Why not a background thread? Is that a potential for this type of leak? What do you recommend using?

Thanks.
mac-dev-7 is offline   0 Reply With Quote
Old Nov 12, 2009, 01:30 PM   #20
mac-dev-7
macrumors newbie
 
Join Date: Nov 2009
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];
}
mac-dev-7 is offline   0 Reply With Quote
Old Nov 12, 2009, 02:15 PM   #21
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
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];
}
PhoneyDeveloper is offline   0 Reply With Quote
Old Nov 12, 2009, 04:54 PM   #22
mac-dev-7
macrumors newbie
 
Join Date: Nov 2009
Thank you so much! That solved the issue.
mac-dev-7 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
button not appearing on XIB Kapthehat iPhone/iPad Programming 11 Sep 4, 2013 09:59 AM
Xcode empty ViewController.xib Pades iPhone/iPad Programming 7 Aug 7, 2013 10:07 AM
How to navigate from one xib to an uitableview(without xib) fypj2013 iPhone/iPad Programming 3 Mar 16, 2013 08:23 AM
XIB And Storyboards nashyo iPhone/iPad Programming 1 Aug 22, 2012 08:42 AM
Xcode 4 mainwindow.xib recobafan iPhone/iPad Programming 4 Jul 6, 2012 12:53 PM

Forum Jump

All times are GMT -5. The time now is 04:42 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC