• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How do I temporary retain self, under ARC?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How do I temporary retain self, under ARC?


  • Subject: Re: How do I temporary retain self, under ARC?
  • From: Dave Fernandes <email@hidden>
  • Date: Thu, 17 Jul 2014 23:47:15 -0400

On Jul 17, 2014, at 11:01 PM, Jens Alfke <email@hidden> 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;

[Conjecture] I haven’t tried it, but maybe the following would work:

__unused id retainedSelf __attribute__((objc_precise_lifetime)) = self;

Now retainedSelf should be retained until it goes out of scope. But I don’t know whether the optimizer is still allowed to mess with this.

> 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 (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


_______________________________________________

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


  • Prev by Date: Re: How do I temporary retain self, under ARC?
  • Next by Date: Re: How do I temporary retain self, under ARC?
  • Previous by thread: Re: How do I temporary retain self, under ARC?
  • Next by thread: Re: How do I temporary retain self, under ARC?
  • Index(es):
    • Date
    • Thread