• 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: Passing param by reference then using within block throws exception
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 > Passing param by reference then using within block throws exception (From: Steve Mills <email@hidden>)

  • Prev by Date: Re: Success with NSTableView weak delegates?
  • Next by Date: Re: Success with NSTableView weak delegates?
  • Previous by thread: Passing param by reference then using within block throws exception
  • Next by thread: Re: Passing param by reference then using within block throws exception
  • Index(es):
    • Date
    • Thread