Anders Sewerin Johansen
Anders Sewerin Johansen

Reputation: 1666

Block + ARC, stack and heap

I want to associate a block with some identifier, and store it in a dictionary for later retrieval.

Am I doing this wrong? Basically I'm worried that I might be passing in a block that is declared on the stack via setAction:forProduct, and the reference will later be invalidated. Is ARC smart enough to catch this?

typedef void (^ProductPurchased)();

-(void) setAction:(ProductPurchased)action forProduct:(NSString*)identifier;

@property (strong, nonatomic) NSMutableDictionary *actions;

-(void) setAction:(ProductPurchased) action forProduct:(NSString*)identifier
{
    [self.actions setObject:action forKey:identifier];
}

Upvotes: 0

Views: 179

Answers (1)

Nick C
Nick C

Reputation: 645

See the answer to this. You need to use Block_copy() before adding it to the dictionary and Block_release() after removing it.

Upvotes: 2

Related Questions