On Thu, 17 Jul 2014 20:01:09 -0700, Jens Alfke said:

>Every once in a while I run into a bug in my code that’s triggered by
>self getting dealloced in the middle of a method. It’s usually something like
>       - (void) someMethod {
>               doSomeStuff;
>               [self.delegate object: self didSomeStuff: yeah];
>               doSomeMoreStuff;
>       }
>where it turns out that the delegate method clears its reference to me,
>and that was the last reference, causing self to get dealloced during
>the delegate call, causing doSomeMoreStuff to crash.

I've had similar cases under garbage collection and one nice solution was to 
add:

[objToKeepAlive self];

at the end.  As the 'self' message does nothing, it's sorta self-documenting, 
and as all message dispatch is dynamic, the optimizer can't remove it.  I don't 
have much experience with ARC yet, but seems this might work there too.

Cheers,

-- 
____________________________________________________________
Sean McBride, B. Eng                 s...@rogue-research.com
Rogue Research                        www.rogue-research.com 
Mac Software Developer              Montréal, Québec, Canada

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to