Re: Need advice: Object ownership and threading issue, causing a rare crash.
Re: Need advice: Object ownership and threading issue, causing a rare crash.
- Subject: Re: Need advice: Object ownership and threading issue, causing a rare crash.
- From: Fritz Anderson <email@hidden>
- Date: Thu, 02 May 2013 10:01:06 -0500
On 1 May 2013, at 7:10 AM, Motti Shneor <email@hidden> wrote:
> - 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.
Probably this is a solved problem, and I'm talking through my hat. As a matter of first impression…
I'd try a broker object. When the MyA is done with the MyB, it hands the MyB over to the broker, which handles the negotiations with the MyB to wind it up, then releases the MyB:
MyA:
hand the MyB over to the broker.
Broker:
is the MyB already finished? Do nothing and return. Otherwise:
retain the MyB (by keeping it in a mutable array?)
tell the MyB that the broker needs a callback when MyB is done (that is, assign the broker as a delegate for this purpose)
tell the MyB to wind itself up
release the MyB and replace it with a new one
MyB:
complete the windup (which may already have happened)
tell the broker it's done
Broker:
release the MyB
I tried sketching it out, but I wasn't going to resolve all the locking and threading issues in the time I can give an email.
If it works at all, it solves the self-release problem, it gives you a one-stop-shopping place to put the asynchrony issues, and I think it handles the irreducible tasks of stopping and disposing of a task the owner no longer wants to deal with.
— F
--
Fritz Anderson
Xcode 4 Unleashed: 4.5 supplement for free!
http://www.informit.com/store/xcode-4-unleashed-9780672333279
_______________________________________________
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