Stephane
Stephane

Reputation: 5078

Is this a retained or autorelease object?

I've declared a MutableArray rqst_entries in the header of my class as property and synthesized it too. In the viewdidload I added the following code:

self.rqst_entries = [NSMutableArray array];

Will it turn the this ivar to be autorelease and maybe release it earlier (before dealloc is called)

Thx for helping,

Stephane

Upvotes: 1

Views: 89

Answers (4)

Sam
Sam

Reputation: 27354

[NSMutableArray array] returns an autoreleased object.

Everybody's answers are about the property rqst_entries. Basically, you need a property that has a memory model of retain. There are instances where you would want to use copy, but this is usually for immutable types (NSArray, NSString, etc...). NSMutableArray is mutable (you can add entries, remove entries, and modify entries in the array).

You want to define your property like:

@property (nonatomic, retain) NSMutableArray *rqst_entries;

The part that really trips people up is doing an alloc / copy / retain in a property assignment like:

self.rqst_entries = [[NSMutableArray alloc] init];

because this will leak unless followed by a release

[self.rqst_entries release];

The best way to assign in this case is to use a local variable (at least if you need thread safety). like:

NSMutableArray *myArray = [[NSMutableArray alloc] init;
self.rqst_entries = myArray;
[myArray release];

I realize this is a VERY trivial example, but it does illustrate the point.

Upvotes: 1

Claus Broch
Claus Broch

Reputation: 9212

If you have defined your property to either retain or copy it will be held by the ivar and you will be fine. If you on the other hand have defined your property to be assigned you will run into problems since [NSMutableArray array] is autoreleased.

Upvotes: 5

jrturton
jrturton

Reputation: 119242

If your property is retained, then you're fine. You've assigned an autoreleased object to your property, but this action has retained it for you.

Upvotes: 3

Fabian Kreiser
Fabian Kreiser

Reputation: 8337

Not if your property is retain or copy.

Upvotes: 3

Related Questions