Re: Appropriate dealloc and finalize actions
Re: Appropriate dealloc and finalize actions
- Subject: Re: Appropriate dealloc and finalize actions
- From: Jens Alfke <email@hidden>
- Date: Sun, 11 Oct 2009 22:03:08 -0700
On Oct 11, 2009, at 7:44 PM, Karolis Ramanauskas wrote:
I have object1 that has a connection to object2, (object1.connection =
object2). And at the same time object2.connection = object1:
O1 -----> O2
O2 -----> O1
It's generally a bad idea to have two objects each retain the other.
It produces a reference loop, which means neither object can be
deallocated without manually releasing each side of the relationship.
Instead, usually one object will be the owner and will retain the
other, but the other won't retain it back. You see this design in the
"delegate" relationships that a lot of Cocoa classes have. The
"delegate" relationship is not retained, to avoid cycles. So my custom
object creates and retains an NSURLConnection, whose delegate is my
object. My class's dealloc method releases the connection object.
In a GC'd app you generally don't have to worry about this kind of
thing. Two objects that point to each other will still both get
collected, once nothing else points to them. There's no need to use a
'finalize' method unless there's some other non-GC'd resource that has
to get cleaned up, like closing a file or free'ing a malloc'ed block.
—Jens_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden