Object-c Easy memory leak when creating objects using class static methods

Source: Internet
Author: User

1. The so-called static method of using a class to create an object, that is, using the class name to call its static method (not explicitly called alloc), you can get a new object, such as the following two examples:

nsstring* str1 = [nsstring stringwithstring:@ "Hello World"];

nsmutablestring* str2 = [nsmutablestring stringwithstring:@ "Hello World"];

2. The first example is to use the literal constant "Hello World" of a string to create a NSString object that is known to be allocated at compile time, stored in the program's data area (constant area), globally unique and immutable Because the NSString object is immutable, str1 directly points to "Hello World" in the data area without creating a copy in memory, and str2 is a mutable string object, so you need to copy the contents of the constant "Hello World" to memory. This supports modifications to the contents of the string. Therefore, the creation of str1 basically does not increase the memory overhead, while the creation of STR2 increases the memory overhead.

The static method of 3.NSString stringwithstring creates an object that, in order to ensure that the object can be disposed correctly, invokes the object's Autorelease method before returning the object, handing the object's release to the outer auto-Release pool object While the corresponding classic Alloc+init method creates an object, the autorelease is not called. Because when you explicitly use Alloc, ARC adds the appropriate release action, so the objects created in this way can be freed normally.

4. Examples of memory leaks

 while (YES) {        nsmutablestring* str = [nsmutablestring stringwithstring@ "HelloWorld" ];  }

The dead loop is used to make the result of the leak more pronounced, and if the data for each operation is large, the effect is more pronounced.

5. Causes of leaks

Every time you create an object, STR will open up a new memory to hold the data "Hello World", and the way to release it is autorelease, but in this loop it is not exposed to the outer layer of the auto-release pool, so all the created objects are not released in a timely manner, so memory consumption will become larger, The effect is like a memory leak.

6. How to Solve

One: Automatic release of the pool within the loop, the timely release of objects (not recommended, increase the automatic release pool object creation and destruction costs)

 while (YES) {      @autorelease {          nsmutablestring* str = [nsmutablestring stringwithstring@ "Hello World"];        }}

II: Create objects using the classic Alloc+init method (recommended, high efficiency without side effects)

 while (YES) {        nsmutablestring* str = [[nsmutablestring alloc]initwithstring:@ 'Hello World "];  }

III: If possible, use NSString instead of nsmutablestring (not recommended, barely circumvented)

 while (YES) {        nsstring* str = [NSString stringwithstring@ "HelloWorld"];  }

Object-c Easy memory leak when creating objects using class static methods

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.