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: Roland King <email@hidden>
- Date: Fri, 18 Jul 2014 11:40:24 +0800
Using the objc_precise_lifetime attribute ensures the object stays alice to the end of the block.
I think that's a bit better than abusing retain release.
> On 18 Jul, 2014, at 11:23, Quincey Morris <email@hidden> wrote:
>
>> 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
_______________________________________________
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