views:

29

answers:

1

I've pounded my head against my monitor for hours (and read similar posts). I'm still stumped.

I declare an array in my *.h file that will display data in a table view:

@interface TeamsTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> 
{
    NSMutableArray *teamsArray;
}

I allocate the array in viewDidLoad and release it in dealloc:

-(void)viewDidLoad 
{
    [super viewDidLoad];

    // Inititalize the mutablearray that will store the xml for the table.
    teamsArray = [[NSMutableArray alloc] init];
}

-(void)dealloc 
{
    [teamsArray release];

    [super dealloc];
}

Each time viewWillAppear, I call my loadData to reload the data (because the data can change). For this post (and as I try to locate my leak), I've hardcoded the data. The comment shows the location of the reported leaks. (The leak occurs when the table view is redisplayed.)

-(void)loadData
{
    // Empty any objects that are already in the array.
    [teamsArray removeAllObjects];

    // Fill a dictionary (normally looping through a file, but hardcoded for leak hunting).
    NSMutableDictionary *oneTeamDictionary = [NSMutableDictionary dictionary];

    [oneTeamDictionary setObject:@"100"         forKey:@"basenumber"];
    [oneTeamDictionary setObject:@"Team Name"   forKey:@"teamname"];
    [oneTeamDictionary setObject:@"XYZ"         forKey:@"teamabbr"];
    [oneTeamDictionary setObject:@"USA"         forKey:@"countryabbr"];
    [oneTeamDictionary setObject:@"Joe"         forKey:@"manager"];

    // Add this team to the array used to display table data.
    [teamsArray addObject:[oneTeamDictionary copy]];  // Leaks Malloc 32 bytes and _NSCFDictionary 48 bytes here.

    // Reload the table view with new data.
    [self.tableView reloadData];
}

In my newbieistic state, I'd think that [teamsArray release] would free the dictionary object. I've also tried using "alloc] init]" to create the dictionary, as well as releasing and reallocating the teamsArray (rather than calling removeAllObjects). Thanks for your help!

A: 

The problem is the copy function. Note from the docs:

Special Considerations

If you are using managed memory (not garbage collection), this method retains the new object before returning it. The invoker of the method, however, is responsible for releasing the returned object.

Why aren't you just calling:

[teamsArray addObject:oneTeamDictionary];
Jonah