small medium large xlarge

Steve_passport_pragsmall
05 Mar 2009, 08:58
Steve Fogel (20 posts)

Hi, all…

In Objective-C 2.0 for iPhone, what is our responsibility with respect to memory management for objects loaded from a NIB? The Apple Memory Management Programming Guide for Cocoa mentions that the owner of the NIB is responsible for releasing its top objects, because these objects are loaded with a retain count of 1. Not sure if this is true for iPhone programming, and not sure I know exactly what the top objects are. Can you shed some light?

Thx

Steve

Biopic_100x100_pragsmall
05 Mar 2009, 13:11
Bill Dudney (917 posts)

Hi Steve,

Each of the objects you can see in the document window in IB are ‘top objects’. Hitting Command-0 will bring the doc window to the front.

The way I try to explain this is that any object you have an outlet to in IB needs to be released in your dealloc method for the controller with the outlet.

If you don’t have an outlet you don’t need to worry about the object.

Please let me know if this does not clear it up.

Good Luck!

Steve_passport_pragsmall
06 Mar 2009, 03:50
Steve Fogel (20 posts)

Thanks, Bill. One more question:

Hi,

Does this mean that Outlets are retained by default on the iPhone?

If so, that means to me that I do not have to include the retain modifier in my property declaration:

@interface myObject : NSObject { IBOutlet UILabel *mylabel; }

@property IBOutlet UILabel *mylabel;

Is this correct? Or does the NIB loading mechanism use my setter and so I must do the retain in my setter, like this:

@interface myObject : NSObject { IBOutlet UILabel *mylabel; }

@property(nonatomic, retain) IBOutlet UILabel *mylabel;

Thx

Steve

Biopic_100x100_pragsmall
06 Mar 2009, 13:06
Bill Dudney (917 posts)

Hi Steve,

If you have a property declared to retain then IB loading mechanism knows you retain it. If not it retains it for you (unless you have a plain setMethod: in which case it assumes you retain it in my experience).

i.e.

@class Foo : NSObject {
  IBOutlet UILabel *_myLabel;
}

</code>

When connected, the IB loading will retain the label for you, so you just need to release in your dealloc.

If you have a property declared to retain, the mechanism knows that you are going to retain so it does not.

Good luck!

Steve_passport_pragsmall
07 Mar 2009, 03:19
Steve Fogel (20 posts)

Hi, Bill…

OK, that’s very helpful. Now one more related question, if you please. Assume I create a view with IB, and that the view has a couple of labels and UIImageViews in it. Assume I have an IBOutlet for the view and IBOutlets for its subviews. I use the outlet for the view itself so I can assign it as a tableHeaderView. I use the outlets for the subviews because I have to set their values.

My guess is that I should declare a property for the view with (retain), but declare my properties for the subviews with (assign). Reason: the view retains its subviews, so I don’t have to. Correctamundo???

Thx

Steve

Biopic_100x100_pragsmall
08 Mar 2009, 03:41
Bill Dudney (917 posts)

Hi Steve,

Yes the subviews list is retained, however I would still make attributes/properties that retain everything that you need to stick around. Then in your dealloc you would release them.

Hope that make sense. If not please ping back.

Generic-user-small
26 Mar 2009, 17:18
rommel manzano (12 posts)

Hi Bill,

How about instance variable do I need to include that also in dealloc? Pls. see code below. Do I need to include myString in deAlloc also?

@class myClass: nsobject{ NSString *myString; }

@property (nonatomic,retain) NSString *myString; @end

Thanks.

Biopic_100x100_pragsmall
26 Mar 2009, 23:18
Bill Dudney (917 posts)

Hi Rommel,

Yep, in your dealloc you should include self.myString = nil;

Keep in mind that its more typical to use copy on NSString’s.

Good Luck!

You must be logged in to comment