Instruments Memory leakage

Source: Internet
Author: User

SELF: it seems that this leaks tool is not always correct. I ran into the program for a while and it showed that there was a red column, but it must have been located in the main function. I asked a buddy in the group that he didn't care too much about it. It may be a system problem. Just let me know first!

 

Although the arc mechanism is added to iOS 5.0 and later versions, memory leakage may still exist due to the complicated mutual reference relationship. Therefore, it is important to understand the principles.

This article describes how to use instruments to find memory leaks in the program and how to use nszombieenabled without arc.

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

Development Environment of the experiment: xcode 4.5.2

1. Run the demo.

Download a demo for Memory leakage: leak app.

Download the program and open it to run. The program is a list of all kinds of sushi rolls. I tried to select several rows in it. It should have crashed when I chose the second row. Crash:

The location where the crash is located is disconnected, but the cause of the crash is unknown.

2. Set nszombieenabled

This is an "exc_bad_access" error. Select nszombieenabled for xcode ". In crash, you may receive more prompts.

Procedure: 1

2: hook

Run the command and select the cell according to the previous operation. Crash again. This time, an error message is displayed in the output window:

13:22:08. 911 propmemfun [2132: 11303] ***-[cfstring respondstoselector:]: Message sent to deallocated instance 0x713ebc0

The message is sent to the released memory. That is to say, the released memory is used. In C, the "wild pointer" is used"

After reading the crash statement, sushistring should be okay. It is initialized from stringwithformat. That is the problem of _ lastsushiselected.

_ Lastsushiselected points to sushistring, which is an autorelease variable. In the second click, sushistring has been released, so crash is used. It will be retained for _ lastsushiselected. The code is modified as follows:

01. <span style = "font-size: 14px;"> _ lastsushiselected = [sushistring retain];

02. </span>

Run, this time does not crash.

3. Analyze memory leakage (SHIFT + command + B)

If the app is no longer crash, check whether there is any memory leakage. Use xcode analyze to analyze where memory leakage exists.

After analysis, you can see:

The following message is displayed, indicating that alertview has not been released and memory has been leaked.

[Alertview release];

After further analysis, the problem is solved.

4. Use the leaks tool of instruments

The Analysis of Memory leakage cannot detect all memory leaks. Some memory leaks are generated only when the user operates. You need to use instruments.

Click the above operation. After the build is successful, the instruments tool jumps out and selects the leaks option. At this time, the sushi program runs. After selecting the items in the list and dragging, the tool displays the following results:

As you may have guessed, the red column indicates Memory leakage. How can I see the leakage through this tool?

First, press the red circle button on the toolbar to stop the tool monitoring memory activity. Select leak, click cross in the middle, and select call tree.

In this case, the options of the call tree in the lower left corner can be selected. Select invert call tree and hide system libraries, which are shown as follows:

At this time, the specific code for Memory leakage is found, and the method specified in the red box on the right shows the memory leakage.

You only need to double-click these methods to jump to the specific code. Haha, isn't it very convenient.

The error 100% indicates that the memory will be leaked.

6. Solve the memory leakage problem

If the problem is found, solve it.

About: tableview: didselectrowatindexpath, analyze its memory process:

  1. The sushistring variable is created through autorelease. Its reference count is 1.
  2. This line of code increases the reference count to 2, _ lastsushiselected = [sushistring retain];
  3. At the end of this method, the autorelease of sushistring takes effect, and the reference count of this variable is reduced to 1.
  4. When the tableview: didselectrowatindexpath method is re-executed, _ lastsushiselected is assigned a new pointer. The reference count of the old _ lastsushiselected is still 1, but it is not released, resulting in Memory leakage.

How can this problem be solved?

In _ lastsushiselected = [sushistring retain]; previously, the original release was OK:

01. [_ lastsushiselected release];

02. _ lastsushiselected = [sushistring retain];

About: tableview: cellforrowatindexpath

This is obvious. sushistring is not released after alloc and init. You can use stringwithformat to call autorelease. The Code is as follows:

01. nsstring * sushistring = [nsstring stringwithformat: @ "% d: % @", indexpath. Row, sushiname];

All right, the leaks are fixed, and you can use tools to analyze them. At this time, you can click, drag, and perform operations without memory leakage. Indicates that the memory leakage is blocked.

Instruments Memory leakage

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.