Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix a memory leak?

After doing a long profile test, I found that in one of my ".m" file memory leak occurs in the viewdidload section. i checked and the xcode highlighted the part where i have initialized picker arrays with values. my program uses pickers for user input. and i have 3 5 different views in my program. the first is a disclaimer ,the second is a menu where the user can choose the type of calculation he/she wants to do. each calculation requires certain inputs which the user enters from a picker. for eg. one of the view has 5 inputs which are handled by 5 different uipickers with seperate arrays for holding the values. these arrays are initialized with the values in the viewdidload method of that view. here is what i found after running the test:

-viewDidLoad ...................................................................................................

instantiation

This is my first time developing an app and i'm kinda confused about what to do. Any help would be appreciated.

like image 871
cyberbemon Avatar asked Aug 30 '11 14:08

cyberbemon


People also ask

What is the main cause of memory leaks?

DEFINITION A memory leak is the gradual deterioration of system performance that occurs over time as the result of the fragmentation of a computer's RAM due to poorly designed or programmed applications that fail to free up memory segments when they are no longer needed.

How do you find a RAM leak?

Check RAM With Windows' Memory Diagnostics Tool Running Windows' Memory Diagnostics is an excellent way to check your computer's physical memory thoroughly for any errors. Press Windows key+R, enter "mdsched.exe," then select OK. Select Restart now and check for problems (recommended).

What is a memory leak and how would you handle it?

In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.


1 Answers

Objects in objective c have a retain count. If this retain count is greater that 0 when the object goes out of scope (when you stop using it), it leaks.

The following things increase the retain count

  • [[alloc] init]
  • new
  • copy
  • [retain]
  • adding an object to an array
  • adding an object as a child (e.g. views)
  • There are likely more, but you don't appear to use any others in your code

The following decrease the retain count

  • [release]
  • removing an object from an array
  • if you dealloc an array, all of its objects are released

You should go through your code and ensure each of the retains or additions to an array are matched with a corresponding release. (You can release member variables in the dealloc method).

EDIT: Jeremy made a valid point that my answer doesn't

Once you add an object to an array, it takes ownership and will release the object when it is done with it. All you need to do is make sure you release anything you own according to the memory management rules

There are also autorelease objects, have a look at this example;

-(init){
    ...
    stagePickerArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < 3; i++)
    {
        //this string is autoreleased, you don't have call release on it.
        //methods with the format [CLASS CLASSwithsomething] tend to be autorelease
        NSString *s = [NSString stringWithFormat:@"%d", i);
        [stagePickerArray addObject:s];
    }
    ...
 }

I think the only thing you are missing is a call to release in your dealloc method

-(void) dealloc
{
    [stagepickerarray release];  //Do this for each of your arrays
    [super dealloc];
}
like image 80
James Webster Avatar answered Sep 19 '22 03:09

James Webster