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