Re: Question Regarding the Memory Address of Objects
Re: Question Regarding the Memory Address of Objects
- Subject: Re: Question Regarding the Memory Address of Objects
- From: Quincey Morris <email@hidden>
- Date: Thu, 18 Dec 2008 12:36:42 -0800
On Dec 18, 2008, at 11:57, Carter R. Harrison wrote:
I have a Nib file setup with an NSView and some NSButton's as
subviews of that view. I have IBOutlets for each of the NSButton's
in my NSView subclass.
In the awakeFromNib: method of my NSView subclass I do the following:
- (void)awakeFromNib
{
//Initialize NSMutableDictionary instance variable
dict = [[NSMutableDictionary alloc] initWithCapacity:1]; // "dict"
is an NSMutableDictionary instance variable
//Set the value "button1" into the dictionary and make it's key the
string representation of button1's memory address.
[dict setValue:@"button1" forKey:[NSString stringWithFormat:@"%x",
&button1]]; // "button1" is one of my IBOutlets.
}
When a user clicks the button the following method fires and
promptly crashes my app.
- (void)buttonClicked:(id)sender
{
// Print out the value for the button pressed. THIS LINE CRASHES
NSString *value = [dict valueForKey:[NSString
stringWithFormat:@"%x", &sender]];
//This next line crashes app - EXC_BAD_ACCESS
NSLog(@"Value is: %@", value);
}
It seems like the memory address of the sender is different than
what it should be. I did some debugging and the address of the
sender is always bfffe2e8 which really shouldn't be possible at all.
Don't use an "&" in either place. You don't want the address of the
outlet or the address of a local variable on the stack, you want a
pointer to the button, which is what you get without the &.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden