Search code examples
objective-cmemory-leaksnsmutablearrayalloc

how to dealloc NSMutableDictionary


I call createTableData from viewDidLoad. What I don't understand is I'm doing an alloc for a NSMutableDictionary but I don't understand why that object is not released from memory-despite the release. I do see memory leaks and Leaks seems to point at this section of code. Can someone point me to a url where I might be able to read/understand what I should be doing versus what I am doing? I just can't seem to see where I've gone wrong here.

- (void)createTableData {
 NSMutableArray *toolList;
 toolList=[[NSMutableArray alloc] init];
 [toolList addObject:[[NSMutableDictionary alloc]
     initWithObjectsAndKeys:@"Some title",@"name",
          @"1",@"whatViewController",
          @"",@"url",
          @"some_icon.jpg",@"picture",
          @"some detail text",@"detailText",nil]];
 toolData=[[NSMutableArray alloc] initWithObjects:toolList,nil];
 [toolList release];
}

- (void)dealloc {
    [toolData release];
    [super dealloc];
}

Solution

  •  [toolList addObject:[[NSMutableDictionary alloc]
         initWithObjectsAndKeys:@"Some title",@"name",
              @"1",@"whatViewController",
              @"",@"url",
              @"some_icon.jpg",@"picture",
              @"some detail text",@"detailText",nil]];
    

    In this line your add NSMutableDictionary object to array and not releasing it. Correct code would be (using class method that already returns autoreleased object):

     [toolList addObject:[NSMutableDictionary 
         dictionaryWithObjectsAndKeys:@"Some title",@"name",
              @"1",@"whatViewController",
              @"",@"url",
              @"some_icon.jpg",@"picture",
              @"some detail text",@"detailText",nil]];
    

    or explicitly autorelease your temporary dictionary:

    [toolList addObject:[[[NSMutableDictionary alloc]
         initWithObjectsAndKeys:@"Some title",@"name",
              @"1",@"whatViewController",
              @"",@"url",
              @"some_icon.jpg",@"picture",
              @"some detail text",@"detailText",nil] autorelease]];