Re: How do I temporary retain self, under ARC?
Re: How do I temporary retain self, under ARC?
- Subject: Re: How do I temporary retain self, under ARC?
- From: Sean McBride <email@hidden>
- Date: Fri, 18 Jul 2014 11:18:06 -0400
- Organization: Rogue Research Inc.
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 email@hidden
Rogue Research www.rogue-research.com
Mac Software Developer Montréal, Québec, Canada
_______________________________________________
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