Snips
Snips

Reputation: 6763

Need to retain an NSArray twice to avoid a crash - why?

I have a complex App that runs reliably, but I'm puzzled why I need to retain a particular NSArray ('sources') twice to prevent a crash (although no exception is reported on the console, but the application crashes and returns to the springboard).

A snippet of the code is included below. There's too much code to paste it all, but you have my word that there are no explicit calls to release the array. 'sources' is an instance variable.

If I only retain the array once (or not at all), I get the crash. With two retains, the App is perfectly stable.

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Sources" ofType:@"plist"];

sources  = [[NSArray arrayWithContentsOfFile:plistPath] autorelease];

[sources retain];
[sources retain];

Thoughts on why I would need to retain this array twice appreciated. Thanks in advance.

Upvotes: 1

Views: 261

Answers (6)

cncool
cncool

Reputation: 11

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Sources" ofType:@"plist"];

sources  = [[NSArray alloc] initWithContentsOfFile:plistPath];

Upvotes: 1

koo
koo

Reputation: 2918

That's because arrayWithContentsOfFile: returns an autoreleased array to you. Calling autorelease on this array will release it twice at the end of current event run loop.

Upvotes: 2

Chuck
Chuck

Reputation: 237060

There is an explicit call to release the array. autorelease is just as explicit as release — it just happens later. Not only that, but it was wrong to autorelease the array in the first place, since you didn't own it. One retain is necessary to claim ownership of the array. The second one prevents the crash by balancing out the incorrect use of autorelease.

Upvotes: 6

drekka
drekka

Reputation: 21883

Ditch the autorelease on the factory method. It's why you need a second retain.

Upvotes: 3

Jeff Kelley
Jeff Kelley

Reputation: 19071

You’re using +arrayWithContentsOfFile:, which returns an autoreleased object, then autoreleasing it again. Take out the call to -autorelease and you’ll be OK. You could re-write it as so:

sources  = [[NSArray arrayWithContentsOfFile:plistPath] retain];

Upvotes: 10

user445929
user445929

Reputation:

Is it something to do with that autorelease? I can't see why that's there: it should be the factory method that autoreleases. Although I don't know what the consequence of adding an extra autorelease is, it might be worth seeing what happens if you take that out along with one of the retains.

Upvotes: 3

Related Questions