Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6017425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:07:36+00:00 2026-05-23T03:07:36+00:00

I’m encountering a strange issue when attempting to access a __block (block mutable) variable

  • 0

I’m encountering a strange issue when attempting to access a __block (block mutable) variable from outside of a block in which it is modified. This is a very toy example that I’m using just to get a better understanding of blocks in general, but currently I have a controller with this method that creates a string with the contents of an NSDictionary which uses NSDictionary‘s enumerateKeysAndObjectsUsingBlock:

- (NSString*) contentsOfDictionary:(NSDictionary*)dictionary
{
    __block NSString *content = @"";

    [dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
        NSString* contentToAppend = [NSString stringWithFormat:@"Object:%@ for key:%@\n", obj, key];
        content = [content stringByAppendingString:contentToAppend];
        NSLog(@"Content in block:\n%@", content);
    }];

    NSLog(@"Content out of block:\n%@", content);

    return content;
}

When I run this method with a dictionary containing the contents:

Value    Key
"Queen"  "card"
"Hearts" "suit"
"10"     "value"

The content variable is modified correctly within the block and I get this output with each iteration:

…Content in block:

Object:Queen for key:card

…Content in block:

Object:Queen for key:card
Object:Hearts for key:suit

…Content in block:

Object:Queen for key:card
Object:Hearts for key:suit
Object:10 for key:value

As soon as the code steps out of the block though, accessing the content string throws an EXC_BAD_ACCESS and on one run occasion it seems to have printed some garbage memory (can’t reproduce)…

What is causing this variable to be deallocated early? I was under the impression that giving it a __block definition would mean that it is retained when used within a block and released when the block exits – But the variable is retained and autoreleased to start off with by virtue of being a string literal so I expect it not to be dealloced until after this method exits at the earliest.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:07:37+00:00Added an answer on May 23, 2026 at 3:07 am

    This is your problem:

    content = [content stringByAppendingString:contentToAppend];
    

    -stringByAppendingString: returns a new, autoreleased object. The address of this object is stored in content. Each go through this (implicit) loop – that is to say, each invocation of the provided block – is creating a brand new object and then assigning the address of that new object to content. None of these objects outlives its containing autorelease pool.

    What you should be doing is using an NSMutableString and directly appending the contentToAppend to the mutable string. For example:

    - (NSString*) contentsOfDictionary:(NSDictionary*)dictionary
    {
        NSMutableString *content = [NSMutableString string];
        [dictionary enumerateKeysAndObjectsUsingBlock:
        ^(id key, id obj, BOOL *stop){
            NSString* contentToAppend = [NSString stringWithFormat:
                @"Object:%@ for key:%@\n", obj, key];
            [content appendString:contentToAppend];
    
            NSLog(@"Content in block:\n%@", content);
        }];
    
        NSLog(@"Content out of block:\n%@", content);
        return content;
    }
    

    Note that __block is no longer necessary, as you do not assign to content anywhere within the block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.