Rocky
Rocky

Reputation: 1423

memory management in objective-c for retain

HI i have questions on Memory Management If I alloc object 1 time then i retain 2 time And then release 4 time & then retain 1 time for same object. what will be my final ans?

Upvotes: 0

Views: 80

Answers (2)

grahamparks
grahamparks

Reputation: 16296

The object will be dealloced after the third release (when the retainCount hits zero) and the fourth release will be sent to a non-valid object, resulting in a crash.

If you want an object to exist for a little while after you've released it, you should look into the autorelease method.

Upvotes: 1

hooleyhoop
hooleyhoop

Reputation: 9198

Crrrrraaassshh!

Upvotes: 3

Related Questions