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.
The __block Storage Type __block variables live in storage that is shared between the lexical scope of the variable and all blocks and block copies declared or created within the variable's lexical scope.
The first rule to avoid retain cycles is that an object must never retain its parent. This changes the previous diagram to the following: This is the easy case: an object should never retain its parent when it makes a pointer to the parent. Notice that the term "weak pointer" is used.
In Objective-C, any character , numeric or boolean literal prefixed with the '@' character will evaluate to a pointer to an NSNumber object (In this case), initialized with that value. C's type suffixes may be used to control the size of numeric literals. '@' is used a lot in the objective-C world.
^blockName: Always remember that the block name is preceded by the ^ symbol. The block name can be any string you like, just like you name any other variable or method. Remember that both the ^ and the block name are enclosed in parentheses ().
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.
Internally, -enumerateKeysAndObjectsUsingBlock:
is using an autorelease pool. __block
scoped objects are not retained past the end of the block's lifetime, so you end up with an object you created in the block's scope that is then deallocated when the dictionary's autorelease pool is drained, which all happens before you attempt to print the value of content
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With