开发者

Obj-C __block variable retain behavior

开发者 https://www.devze.com 2023-03-13 01:55 出处:网络
I\'m encountering a s开发者_开发知识库trange 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 us

I'm encountering a s开发者_开发知识库trange 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消