Alex Rivera | Logout

Setting an object nil versus release+realloc

Asked 2009-03-09T16:03:52.267
9

This is not a garbage collected environment

I have a class instance variable which at some point in my runtime, I need to re-initialize with a different data set than it was originally constructed with.

Hypothetically speaking, what if I have an NSMutableArray or an NSMutableDictionary, would it be more efficient to do something such as:

[myArr release];
myArr  = [[NSMutableArray alloc] init....];

or just,

myArr = nil;

Will myArr release the object and leave me without a pointer to the storage in memory so I can re-use myArr?

Edit
Report

1 Answer

6

You could use a property and get almost the syntax you want without the memory leak.

Use this syntax to declare the array

@property (readwrite, retain) NSMutableArray *myArray;

Then re-initialize it like this:

[self setMyArray:[NSMutableArray array]];
answered 2009-03-09T16:59:37.170

Your Answer