Search code examples
iphonememory-leaksinstruments

Check for memory leak


Hi please tell me if there is any memory leak in my code or not..
I am solving my memory leaks in my app but confused here will it create a memory leak?

     NSMutableArray *dataArray=[[NSMutableArray alloc]init];

if(condition)
{
        [dataArray addObject:[appDelegate selectFromDatabase:x]];//returning an autoreleased array
}
else
{
dataArray=[appDelegate selectFromDatabase:a];
}
     //use dataArray

        [dataArray release];

Solution

  • This code will leak memory. You reassign dataArray without releasing the old value. Do the assignment through a generated property and the old value will be released first.

    else
    {
        dataArray=[appDelegate selectFromDatabase:a];
    }
    

    You could also restructure your code to avoid creating the array unless you really need it:

    if (condition)
    {
        // Create array here
    }
    else
    {
        // get array from other place
    }