Crash using block stored in mutable dictionary

Joined
Aug 27, 2011
Messages
8
Reaction score
1
Points
3
What can people tell me about why this program crashes?

Code:
#import <Foundation/Foundation.h>

@interface Action : NSObject 
+ (void)execute;
@end

@implementation Action
+ (void)execute
{
	NSLog(@"The action was executed.");
}
@end

@interface ActionPerformer : NSObject {
@private
	NSMutableDictionary* _namedActions;
}

+ (ActionPerformer*) performer;
- (void)addAction:(NSString*)action withClass:(Class)class andSelector:(SEL)selector;
- (void)performAction:(NSString*)action;
@end

@implementation ActionPerformer

typedef void(^ActionBlock)();

+ (ActionPerformer*) performer
{
	ActionPerformer* performer = [[ActionPerformer alloc] init];
	performer->_namedActions = [NSMutableDictionary dictionary];
	return performer;
}

- (void)addAction:(NSString *)action withClass:(Class)class andSelector:(SEL)selector
{
	ActionBlock block = ^() { [class performSelector:selector]; };
	// Verify things work
	block();
	[_namedActions setObject:block forKey:action];
}

- (void)performAction:(NSString *)action
{
	ActionBlock block = [_namedActions objectForKey:action];
	// Crash happens here!!!
	block();
}

@end

int main()
{
	@autoreleasepool {
		ActionPerformer* performer = [ActionPerformer performer];
		
		[performer addAction:@"action" withClass:[Action class]
			andSelector:@selector(execute)];
			
		[performer performAction:@"action"];	    
	}
    return 0;
}
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top