views:

56

answers:

2

Hi all,

I've got a memory leak in Instruments... I've sorted some before, but this one has left me stumped! I would be very grateful if you could help... This is the method with the leak... It takes in a dictionary of data, creates a new based on it and returns it. I've commented the lines with the leaks, and the percentages it gives the leak (not sure what that's all about), I've tried to clean things up a bit by choosing to alloc/init/release instead of autorelease, but that doesn't seem to make any difference...

- (PetalLayerView *)makePetalLayerWithData:(NSDictionary *)sectionData isZeroIndexed(BOOL)zeroIndexed
{
    NSMutableSet *petalsData = [[NSMutableSet alloc] init];       // 7.2%
    NSArray *sections = [sectionData objectForKey:@"sections"];

    NSNumber *startIndex, *endIndex;
    NSDictionary *petalData;
    for(int i=0; i<sections.count; i++)
    {
        startIndex = [sections objectAtIndex:i];

        if(i < sections.count - 1)
             endIndex = [sections objectAtIndex:i+1];
        else
             endIndex = [sections objectAtIndex:0];

        if(!zeroIndexed)
        {
             startIndex = [NSNumber numberWithInt:[startIndex intValue]-1];   // 10.2%
             endIndex   = [NSNumber numberWithInt:[endIndex   intValue]-1];   // 10.5%
        }

        petalData = [[NSDictionary alloc] initWithObjectsAndKeys:startIndex, @"startIndex", endIndex, @"endIndex", nil];   // 64.4%
        [petalsData addObject:petalData];   // 7.7%
        [petalData release];
   }

   int maxLength = MAX(self.frame.size.width, self.frame.size.height);
   CGRect petalFrame = CGRectMake((self.frame.size.width - maxLength)/2, (self.frame.size.height - maxLength)/2, maxLength, maxLength);
   PetalLayerView *petalLayerView = [[[PetalLayerView alloc] initWithFrame:petalFrame] autorelease];

   NSString *tagGroupName = [sectionData objectForKey:@"section_name"];

   WheelModel *wheelModel = [WheelModel sharedInstance];

   if([sectionData objectForKey:@"filtered"])
   {
        petalLayerView.outlineColor = [wheelModel.tagColors objectForKey:tagGroupName];
   }
   petalLayerView.petalColor = [wheelModel.petalColors objectForKey:tagGroupName];
   petalLayerView.petalsData = petalsData;
   [petalsData release];

   return petalLayerView;
}

Any help much appreciated! Thanks!

:-Joe

A: 

You need to release startIndex and endIndex just after petalData = [[NSDictionary alloc] ...

John Drummond
No, those newly created are `autoreleased` and those obtained from the array are not owned either. See e.g. [Object Ownership Policy](http://developer.apple.com/mac/library/documentation/cocoa/conceptual/MemoryMgmt/Articles/mmObjectOwnership.html#//apple_ref/doc/uid/20000043-SW1).
Georg Fritzsche
+1  A: 

Are you sure you're leaking memory? petalsData is getting retained (presumably) by petalLayerView, so all the data that's being allocated isn't supposed to go away (ie. it's not a leak, it's intentionally allocated). It's possible the view itself is being leaked, I supposed, but that would be outside the scope of the supplied code.

samkass
Thanks... I found this out just as you wrote this answer. The leak wasn't there at all, but in the PetalLayerView (as you suggested). The PetalLayerView wasn't releasing any of its ivars in the dealloc... Me being stupid, I didn't realise that I had to follow the leaks through to connected objects.Thanks for your help :)
Joe