• 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: Quincey Morris <email@hidden>
  • Date: Thu, 17 Jul 2014 20:23:48 -0700

On Jul 17, 2014, at 20:01 , Jens Alfke <email@hidden> wrote:

> 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

This seems to be the right way to do it. A CFBridgingRetain/CFBridgingRelease pair may be a slightly prettier way of achieving the same effect.

It’s always going to be ugly to some degree, because the solution you’re using isn’t the solution to your problem, it just fixes your problem “accidentally" as a side effect.

> and could cause leaks if the method returns early.

Perhaps the following pattern would work:

	- (void) someMethod {
		CFTypeRef selfRef = CFBridgingRetain (self);
		@try {
			doSomeStuff;
			[self.delegate object: self didSomeStuff: yeah];
			if (somethingWentWrong)
				return;
			doSomeMoreStuff;
		}
		@finally {
			CFBridgingRelease (selfRef);
		}
	}

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

Your real problem is that you don’t have a safe mechanism for ensuring the object’s lifetime. If you solve that, you won’t have to worry about any of the above.

_______________________________________________

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


  • Follow-Ups:
    • Re: How do I temporary retain self, under ARC?
      • From: Roland King <email@hidden>
  • Prev by Date: Re: showing app windows at launch prior to NSDocument tasks
  • Next by Date: Re: How do I temporary retain self, under ARC?
  • Previous by thread: Re: iBeacons - can 100 different devices be active and seen by one device?
  • Next by thread: Re: How do I temporary retain self, under ARC?
  • Index(es):
    • Date
    • Thread