Need advice: Object ownership and threading issue, causing a rare crash.
Need advice: Object ownership and threading issue, causing a rare crash.
- Subject: Need advice: Object ownership and threading issue, causing a rare crash.
- From: Motti Shneor <email@hidden>
- Date: Wed, 01 May 2013 15:10:19 +0300
Hello.
I have two NSObject subclasses - say MyA and MyB.
- Each MyA instance. creates and owns a MyB instance.
- MyB instances create an NSThread, and live their asynchronous life, communicating with a remote internet server, and sometimes with their owner (the MyA object) who lives in the main thread.
- It can happen that MyA wants to dispose of its MyB, (in the main thread), and replace it with another instance of MyB.
- However, MyB cannot be released instantaneously, or synchronously, because its thread should gracefully close and disconnect from the server, which takes long seconds and even more.
Currently, MyB retains itself within its thread, and releases itself just before the thread exits. I know it is bad practice (self retaining objects), but I did not yet think of a better way.
This way, when the owner releases its MyB and replaces its reference with a new MyB object, the MyB is just released once, and does not get deallocated. When its thread finishes its cycle in the background, the thread function releases the objects again, and returns.
This seems to be OK --- but in rare cases the program crashes trying to REENTER the "dealloc" of MyB!!!! It seems that the actual "release" method gets called both from the thread context and the main thread's context.
What's the best way to get out of this? How can I simply synchronize the "release" calls, without blocking both threads? Maybe I should restrict myself to retaining/releasing in the main thread only? Is there some syntactic Obj-C sugar to ease such things?
Thanks!
Motti Shneor, Mac OS X Software Architect & Team Leader
Spectrum Reflections Ltd.
+972-54-3136621
_______________________________________________
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