Re: NSDictionary allocation
Re: NSDictionary allocation
- Subject: Re: NSDictionary allocation
- From: "Adam R. Maxwell" <email@hidden>
- Date: Mon, 14 May 2007 18:40:53 -0700
On May 14, 2007, at 16:44, Boyd Collier wrote:
I want to allocate an NSDictionary and initialize its contents with
arguments passed to it, and to do this within the initialization I
make use of an NSMutableDictionary. But I want the dictionary
that's returned from my initialization to be an NSDictionary rather
than the NSMutableDictionary.
@interface AlleleDictionary : NSDictionary {
}
- (id)initWithSpagediData:(SpagediData*)spagediDataObj locus:
(int)locus;
@end
@implementation AlleleDictionary
- (id)initWithSpagediData:(SpagediData*)spagediDataObj locus:
(int)locus {
if ((self = [super init])) {
NSMutableDictionary *theAlleleDict = [[NSMutableDictionary alloc]
initWithCapacity:[spagediDataObj numberOfIndividuals] ];
// code that puts stuff into theAlleleDict (not shown)
self = [[NSDictionary alloc] initWithDictionary:
(NSMutableDictionary*)theAlleleDict copyItems:YES];
Your suspicion was correct; you are reassigning self without releasing
the result of +alloc first. Send [self release] before this line and
you should be okay. What we generally do in this case is discard that
object immediately, as in
- (id)customInit {
[[self init] release];
self = [[RealObject alloc] init];
if (self) {
// init stuff
}
return self;
}
so it's immediately clear that you're discarding the original object.
--
Adam
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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