Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: NSMutableDictionary autorelease chrashes application
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSMutableDictionary autorelease chrashes application



In the code you provide, the first line allocates a brand new dictionary and assigns it to vAttributes.

In the next line, you reassign vAttributes to the contents of your iVar dictionary. Nothing points to that alloc'd dictionary in the first line anymore.

Since vAttributes now points to an autoreleased dictionary, attempting to release it will cause a crash.

Try something along these lines instead:

- (NSString *)descriptionByColorCode:(int)colorCode
{
NSDictionary * vAttributes = [vColors objectForKey:[NSNumber numberWithInt:colorCode]];
return [vAttributes objectForKey:@"colorDescription"];
}



On Jul 18, 2008, at 9:48 AM, Matthias Arndt wrote:

Hi!

I'm a rookie with Cocoa development, please excuse if this question is stupid, but I'm struck with memory management (an even Aaron's book doesn't help me):

In a method I use a (temporary) dictionary "vAttributes" to read an object from an instance variable "vColors" (a dictionary, too):

- (NSString *)descriptionByColorCode:(int)colorCode
{
NSMutableDictionary *vAttributes = [[NSMutableDictionary alloc] init];

vAttributes = [vColors objectForKey:[NSNumber numberWithInt:colorCode]];
return [vAttributes objectForKey:@"colorDescription"];
}


It is my understanding, that after the "alloc" message "vAttributes"' retain count is 1 and as I use it in the return statement, a "release" out of question. So "vAttributes" should receive an "autorelease" message before the return statement to avoid an object leak, but than the application crashes after the first invokation of this method.

... where am I wrong?

_______________________________________________

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


References: 
 >NSMutableDictionary autorelease chrashes application (From: Matthias Arndt <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.