Re: How do I guarantee that an object is dealloced on the main thread?
Re: How do I guarantee that an object is dealloced on the main thread?
- Subject: Re: How do I guarantee that an object is dealloced on the main thread?
- From: Brian Stern <email@hidden>
- Date: Mon, 6 Oct 2008 10:05:04 -0400
Ok, I've fixed this as recommended. It really wasn't too difficult to
move all the interesting bits of dealloc to the stop method, and to
zero out all the instance variables after they're released so this can
be safely called more than once. This way I can call stop from the
main thread and not worry about what thread dealloc will be called on.
I didn't write this code, it's a freely available server
implementation, and it wasn't designed to be dealloced. It is
intended to just run until the app terminates, but that doesn't meet
my needs.
Thanks for the input.
Brian
On Oct 5, 2008, at 6:08 PM, Jim Correia wrote:
On Oct 5, 2008, at 5:41 PM, Brian Stern wrote:
My main reason was just cleanliness. Looking more closely at the
code however, most of which I didn't write, there are some runloop
sources that are removed and calls to NSObject
cancelPreviousPerformRequestsWithTarge:selector:object. Most of the
code that set those things up will have run on the main thread so
should be cleaned up on the main thread. There's some other
networking cleanup that I don't know if it's threadsafe or not.
It's conceivable that I can rework the cleanup so that most of it
happens in the stop method and the dealloc method is mostly empty
That's probably the preferable solution to this problem, even if it
is more work up front.
I guess from your question you don't have a one sentence answer to
my question.
Using classic memory management, your object will be deallocated
when its last owner releases it. If this object has been shared
amongst threads, the it will be deallocated on the last thread which
releases it.
Under garbage collection, you are encouraged not to implement a -
finalize method. If you do implement one, there is no guarantee
about the order it will be called in, if it will be called at all,
or what thread it will be invoked on.
In your -stop method, you know you are done with the object, so
doing your cleanup there (on the main thread, if necessary) seems
like an appropriate solution.
- Jim
_______________________________________________
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