Re: Passing param by reference then using within block throws exception
Re: Passing param by reference then using within block throws exception
- Subject: Re: Passing param by reference then using within block throws exception
- From: Doug Hill <email@hidden>
- Date: Tue, 20 Sep 2016 14:45:00 -0700
I think this is the exact solution. As I'm sure you've tried, you can't use the __block modifier on method parameters, as you will get the following compiler error:
__block attribute not allowed, only allowed on local variables
So, you're doing the right thing creating the local as a temporary to eventually assign to the method parameter.
Doug Hill
> On Sep 20, 2016, at 2:06 PM, Steve Mills <email@hidden> wrote:
>
> I'm turning on ARC for a project (yay) and have run into a problem I can't wrap my head around. It always worked fine before ARC. When I turn zombies on, doing "memory history 0x610004279ac0" can't find it in the history. Here's the method and the call to it:
>
> -(void) doStuff:(NSString**)fillMeIn
> {
> [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL* stop) {
> if(obj.flag) {
> *stop = YES;
> *fillMeIn = @"wow";
> }
> }];
> }
>
> NSString* getFilledIn;
>
> [thing doStuff:&getFilledIn];
>
> The call to doStuff: results in EXC_BAD_ACCESS, or "*** -[CFString retain]: message sent to deallocated instance 0x610004279ac0" if I turn zombies on.
>
> I tried changing the param type to (NSString** _Nonnull), thinking it was confused about my knowing that the reference will never be nil, but it didn't help. Then I got to thinking about the reference being assigned inside the block and changed it to:
>
> -(void) doStuff:(NSString** _Nonnull)fillMeIn
> {
> __block NSString* noFillMeIn;
>
> [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL* stop) {
> if(obj.flag) {
> *stop = YES;
> *noFillMeIn = @"wow";
> }
> }];
>
> *fillMeIn = noFillMeIn;
> }
>
> That seems to fix it. Is there a better way to deref and assign to the param from within the block?
_______________________________________________
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