New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Crash using block stored in mutable dictionary


Post Reply New Thread Subscribe

 
Thread Tools
tron_thomas

 
Member Since: Aug 27, 2011
Posts: 6
tron_thomas is on a distinguished road

tron_thomas is offline
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;
}
QUOTE Thanks

Post Reply New Thread Subscribe


« Inline Assembly in XCode 4.0 | I have an idea for an App!...advice needed. »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Dictionary has gone!!!! There is just wikipedia!!! macobsession OS X - Apps and Games 14 06-25-2011 11:23 AM
Intro and some problems. USMCcelica Apple Notebooks 14 11-20-2008 12:58 AM
iDVD crash, crash, crash, crash! jacksonworld OS X - Apps and Games 3 08-21-2006 03:03 PM

All times are GMT -4. The time now is 06:46 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?