I still have some unclear understand about release and autorelease. What are the difference between both of them? I have this code. For facebook connection. I crash it sometimes when I go to Facebook login, I doubting maybe it is because I don’t release the object nicely.? Thanks for any helps
if (_session.isConnected) {
[_session logout];
} else {
FBLoginDialog* dialog = [[[FBLoginDialog alloc] initWithSession:_session] autorelease];
[dialog show];
}
The Memory Management Programming Guide for Cocoa will soon be your best friend. In brief, object instances in Cocoa are memory managed using reference counting (unless, of course you’re using garbage collection on OS X). An object indicates that it wants to ‘retain’ an ownership interest in an other instance–keep it from being deallocated–by sending it a
-retainmessage. An object indicates that it wants to release that interest by sending the other instance a-releasemessage. If the number of objects that have ‘retained’ and ownership interest in an object drops to 0 (i.e. when the last of the owning instances sends a-releasemessage), the instance with a 0 retain count is deallocated.It’s sometimes convenient to say “I want this instance to be released some time in the future“. That’s the purpose of
-autorelease. Sending an-autoreleasemessage adds the receiver to the currentNSAutoreleasePool. When that pool is drained, it sends a-releasemessage to all the instances in the pool. AnNSAutoreleasePoolis automatically created at the start of each iteration of each thread’s run loop and drained at the end of that iteration. Thus, you can do something like this in a method:The caller of this method will get back an instance that they can
-retainif they wish to keep it. If they don’t retain it, it will stick around at least until the enclosing autorelease pool is drained: