user598241
user598241

Reputation: 91

Xcode memory leak

I have a memory leak when I call the following method

- (NSArray *) children
{
NSArray *children = [node objectForKey:TFHppleNodeChildrenKey];
NSMutableArray *hpple = [NSMutableArray arrayWithCapacity:[children count]];
for(NSDictionary *child in children) {
    [hpple addObject:[[TFHppleElement alloc] initWithNode:child]];
    [child release];
}
return hpple;
}

I get a memory leak on TFHppleElement, I alloc this but I not sure of the best way to release it in this context? The TFHppleElement initWithNode looks like this:

- (id) initWithNode:(NSDictionary *) theNode
    {
 if (!(self = [super init]))
  return nil;

[theNode retain];
node = theNode;

return self;
 }

Upvotes: 0

Views: 260

Answers (2)

PRacicot
PRacicot

Reputation: 106

Not shure cause I haven't started Objective-C not to long ago but I would to autoreleas directly when you alloc it. That will prevent you from having to manage the release and making the runtime manage the release of it.

- (NSArray *) children
{
NSArray *children = [node objectForKey:TFHppleNodeChildrenKey];
NSMutableArray *hpple = [NSMutableArray arrayWithCapacity:[children count]];
for(NSDictionary *child in children) {
    [hpple addObject:[[[TFHppleElement alloc] initWithNode:child] autorelease]];
    [child release];
}
return hpple;
}

Upvotes: 0

Vladimir
Vladimir

Reputation: 170839

You can simply autorelease that object to make runtime responsible for releasing it:

[hpple addObject:[[[TFHppleElement alloc] initWithNode:child] autorelease]];

Upvotes: 3

Related Questions