Do you know this code will always run on the main thread? Because you could
fix this from the other side, in the delegate. Use
dispatch_async(dispatch_get_main_queue(), ...) to un-assign the property.
But this won't work if you're on a non-main thread when you call back into
the delegate.


On Thu, Jul 17, 2014 at 10:01 PM, Jens Alfke <j...@mooseyard.com> wrote:

> 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.
>
> Once I’ve identified such a bug, the fix is easy: put a [[self retain]
> autorelease] at the top of the method. Except now I’m using ARC, and I
> can’t find a simple way of doing it. I tried adding
>         __unused id retainedSelf = self;
> but the optimizer recognizes that retainedSelf isn’t used and strips it
> out, making this a no-op. The only thing I’ve found that works is
>         CFRetain((__bridge CFTypeRef)self);
> at the top of the method, and a corresponding CFRelease at the end, but
> this is pretty ugly and could cause leaks if the method returns early.
>
> Am I missing some convenient way of doing this? I looked through the ARC
> docs, at least those I could find, and didn’t see anything relating to this.
>
> —Jens
> _______________________________________________
>
> 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/stephen.butler%40gmail.com
>
> This email sent to stephen.but...@gmail.com
>
_______________________________________________

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