Detecting memory leaks with instruments

Source: Internet
Author: User

Tags: Xcode original works, allow reprint, when reproduced please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. Http://dawn110110.blog.51cto.com/3049492/899283IPhone on the wood has garbage collection, dynamic application of memory to remember to release, this article summarizes the possible memory leaks in various situations, as well as detection methods. Memory leaks plainly, there is such a dynamic request for memory, but the wood has any one pointer pointing to it. For example, in C + +:
    1. MyClass * foo = new MyClass;
    2. foo = NULL;
Similarly, in the objective-c:
    1. MyClass *foo = [[MyClass alloc] init];
    2. foo = nil; //Memory leaks!
Or have a class like this:
  1. @interface Foo:nsobject
  2. {}
  3. @end
  4. @implementation Foo
  5. -(ID) init
  6. {
  7. if (self = [super init])) {
  8. nsmutablestring * string = [[Nsmutablestring alloc] initwithstring:@"leaker!!!"];
  9. string = nil; //leaked out! After that, the string object will not be freed until the application exits.
  10. }
  11. return self;
  12. }
  13. -(void) Dealloc
  14. {
  15. [Super Dealloc];
  16. }
  17. @end
What happens when a memory leak occurs? A: Very small leak, nothing! The memory used when the app exits is cleared, including leaked, and the iphone app does not request shared memory, does not ripple across the system, and there is no system-wide memory leak. If there are more leaks and repeated memory leaks, the application crashes at most. However, if you want to publish to AppStore, there is a memory leak will generally be rejected by Apple .... Let's take a look at how memory is managed in the iphone. First: Wood has automatic garbage collection! Second: There are citation counts! The NSObject object has a property: Retaincount, so all objects inherit from NSObject, only a unified reference count .... Then keep in mind the following 8 things:
    • 1 this retaincount = = 0, the object's Dealloc method is called and the object is freed.
    • 2 Never Show Call Dealloc method
    • 3 [obj retain]; obj.retaincount++ Citation Count +1
    • 4 [obj release]; obj.retaincount--Reference Count-1
    • 5 MyClass * obj = [[MyClass alloc] init];//obj.retaincount = 1 initial value is 1
    • 6 for attributes with retain properties such as the following:
  1. @interface Myclass:nsobject {
  2. nsobject* foo;
  3. }
  4. @property (noatomic,retain) nsobject * FOO;
  5. @end
  6. @implementation MyClass
  7. @synthesize foo;
  8. ...
  9. //The following two lines are the correct wording
  10. Self.foo = bar; //[foo release]; foo = bar; [Foo retain]; Will send a release to the original object, send a retain to the new object
  11. Self.foo = nil; //[foo release]; a release message will be sent to the object
  12. //The following line is wrong
  13. foo = bar;  //did not send the release to Foo did not send retain to bar, this writing is wrong.
  14. ...
  15. @end
Reference:
An
explanation of Apple's documentation and specific implementations
    • 7 for Stringwithstring This method to create the object, a Runloop end, the object will be sent a release message, note that there are two points, one is to pay attention to understanding runloop, to avoid the end of the Runloop object is released and use the object, two is If you use the instruments tool to detect memory leaks, this use of autorelease will also be treated as memory leaks, pay attention to identify ....
    • 8 for Nsmutablearray This kind, when AddObject, when the corresponding is added to the array of the object's reference count will be + 1, when the array itself is released, it will first send a release message to all objects in the array.
Instruments can be used to detect memory leaks, in a variety of tools, with leaks. Easy to use: use Xcode (4.3) to open the corresponding project, Product->profile, and choose the leaks tool. It then dynamically records where the application is running and where memory leaks occur. There are two things to be aware of: 1 for objects that are autorelease, the leak tool also sees it as a leak, knowing that it is OK. 2 Remember to open extended Detail, for each leak, you can see the call stack here, double-click on an item in the call stack, you can directly navigate to the code, easy to find errors and modifications. The situation is as follows two graphs:

This article is from the "I have become a demon" blog, please be sure to keep this source http://dawn110110.blog.51cto.com/3049492/899283

Detecting memory leaks with instruments

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.