Chaosphere2112
Chaosphere2112

Reputation: 674

Objective-C NSString Autorelease Issue

I just spent the last two hours slamming my forehead against my code, trying to figure out what was wrong with it. I eventually narrowed it down to one pair of lines.

NSArray *components = [string componentsSeparatedByString:@" "];

if ([components count] > 1){                        
    retainedProperty1 = [header objectAtIndex:0];

    retainedProperty2 = [header objectAtIndex:1];
}

Whenever I attempted to access either of these fields after a delay in execution (I'm using CocoaAsyncSocket, and it would pull in the rest of the data I needed for this object in a second read from the socket), they would both have become zombies. I was just wondering why the heck this was happening?

Upvotes: 1

Views: 101

Answers (1)

Lily Ballard
Lily Ballard

Reputation: 185681

You're lying to yourself. You're saying retainedProperty1 = ... but you're actually assigning to the ivar, not the property. Unless you're using ARC (and you're not or you wouldn't have this problem), this doesn't do any memory management whatsoever. It won't even free the previous value of this ivar. You should be saying

self.retainedProperty1 = ...
self.retainedProperty2 = ...

Upvotes: 5

Related Questions