Analyze app memory leaks with leaks templates

Source: Internet
Author: User

Although the arc mechanism was added after iOS 5.0, a memory leak might exist due to the complexity of the mutual reference relationship. So it's important to understand the principle.

Here's how to use instruments to find memory leaks in programs and use nszombieenabled settings without arc.

This article assumes that you are already familiar with the OBJ-C memory management mechanism.

Experimental development environment: XCode 4.5.2

1, run the demo.

Download a demo that implements a ready-to-go memory leak: Leak app

Download down, open run, the program is a sushi list that lists various sushi rolls. Try to choose a few lines inside, it should be the second row when the collapse. Collapse:


In the collapse of the place, know crash place, but do not know the reason for the specific crash.

2. Set nszombieenabled

This is a "exc_bad_access" error. We open the Xcode option: "Nszombieenabled". You may be given more tips when crash.

Setup steps: 1


2: Check the red box.


Run, select the cell in the operation as you just did. Crash again, this time in the Output window you will see an additional error message:

2012-11-28 13:22:08.911 propmemfun[2132:11303] * * *-[cfstring respondstoselector:]: Message sent to deallocated instance 0x713ebc0

It probably means: send a message to the freed memory. In other words, the freed memory is used, which is equivalent to using the "wild pointer" in the C language.


look at the next crash of this statement, sushistring should be no problem, it is from stringWithFormat initialized. That's the problem with _lastsushiselected.

_lastsushiselected points to the sushistring,sushistring is a autorelease variable. In the second click, Sushistring has been released, so crash. Keep it for _lastsushiselected, you can use it. The code is modified as follows:

[CPP] view Plaincopy
    1. <span style= "FONT-SIZE:14PX;"  > _lastsushiselected = [sushistring retain];
    2. </span>

Run, and this time does not crash.

3. Analyze memory Leaks (SHIFT+COMMAND+B)

The app isn't crash, so see if there's a memory leak. With Xcode's analyze, you can analyze where there's a memory leak.


After analysis, you can see:


Here is a hint that Alertview is not released, there is a memory leak, then we release

[Alertview release];

Further analysis, this problem solved.

4. Using Instruments's leaks toolThe analysis of memory leaks does not reveal all memory leaks, and some memory leaks are generated when the user is operating at runtime. Then we need to use the instruments.
According to the above operation, build success out of the instruments tool, select the leaks option, this time the sushi program is also running, check the list of items, drag, and so on, the tool shows the following:
 as you can probably guess, a red pillar indicates a memory leak. How did you see the leak through this tool?start by pressing the Red Circular button on the toolbar to stop the tool monitoring memory activity. Select Leak, then click the Middle cross to cross that, select Call Tree.  The option to call tree in the lower left corner is optional. Select Invert call Tree and hide System Libraries, as shown below: The specific code for the memory leak is found, and the red box on the right specifies which method has a memory leak. you just double-click on these methods, will jump to the specific code, haha, is not very convenient.
here should be a hint 100% memory will leak.  6. Resolve Memory LeaksThe problem is found, so solve it.about: Tableview:didselectrowatindexpath, analyzing its memory process:

    1. The sushistring variable is created by Autorelease and its reference count is 1.
    2. This line of code causes the reference count to increase to 2, _lastsushiselected = [sushistring retain];
    3. At the end of this method, Sushistring's autorelease takes effect, and the reference count for this variable is reduced to 1.
    4. When the Tableview:didselectrowatindexpath method is executed again, _lastsushiselected is assigned a new pointer, the old _lastsushiselected reference count is still 1, not released, A memory leak has been generated.

How to solve it?

In _lastsushiselected = [sushistring retain]; Before the original release is OK:

[CPP] view Plaincopy
    1. [_lastsushiselected release];
    2. _lastsushiselected = [sushistring retain];

 about: Tableview:cellforrowatindexpath

This is more obvious, Sushistring is not released after Alloc and INIT, you can call Autorelease with stringWithFormat, the code is as follows:

[CPP] view Plaincopy
    1. nsstring *sushistring = [NSString stringwithformat:@"%d:%@", Indexpath.row, Sushiname];


OK, leaks are fix, and then use the tool analysis to see, this time you point, and then drag, and how to operate, there is no memory leaks. Indicates that the memory leak is blocked.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.