Cocoa Touch a question. Should I save the [NSMutableArray array]?

This is the essence of some code that I am writing. I am worried that I am not correctly looking at persistence / release issues using the array class method in NSMutableArray. Is the following a memory leak?

for(a while) { // do stuff NSMutableArray *a = nil; // do stuff if (!a) { a = [NSMutableArray array]; } } // for(a while) 
+4
source share
4 answers

There will be no memory leak in this code, and freeing the array itself will crash when the array is auto-implemented at the end of the run loop.

Most Cocoa classes provide several ways to create a new object and are very consistent with this convention:

  • [[NSSomeObject alloc] init] : you are responsible for freeing the object (instance method).

  • [NSSomeObject someObject] : the object will be auto-implemented for you, usually at the end of the execution loop (class method). This is roughly equivalent to [[[NSSomeObject alloc] init] autorelease] .

Proper use of the instance method will be:

 a = [[NSMutableArray alloc] init]; // do stuff [a release]; 

Proper use of the class method will be:

 a = [NSMutableArray array]; // do stuff, array is in the autorelease pool 

Please note that Apple recommended avoiding convenient methods to improve performance as much as possible. This is controversial advice , cannot save a lot of CPU time and separates alloc-init from release on an object that you might not really care about storage.

+12
source

From Cocoa Memory Management Rules :

You get ownership of the object if you create it using a method whose name begins with "alloc" or "new" or contains "copy" (for example, alloc, newObject or mutableCopy), or if you send to save the message. You are responsible for refusing to own your own property through release or auto-advertising. At any other time, when you receive an object, you should not let it go.

Therefore, with the line:

 a = [NSMutableArray array]; 

you do not take responsibility for the array, and it will be transferred to you automatically. The memory will be processed automatically automatically by the autostart pool, and as soon as it is no longer used, it will be released for you. However, if you want to save the array outside the current event, you must save it, otherwise it will be released for you.

+6
source

Yes, if you want him to stay.

The returned object is an automatically released object that will be released after the automatic release pool is cleared.

All array class methods starting with "array" return these types of automatically issued objects.

Read this document from Apple.

+2
source

It's really. This can help simply manage things manually when you have questions to find out.

There is an agreement:

  • initialization prefixes (init, initWithString :) indicate the number of holds 1, where
  • The prefix objectname (string, stringWithString :) indicates an object with automatic implementation

I had the habit for many years to release what I can on the call site, instead of clicking on it to auto-implement. Some problems with abstracts become painfully difficult to track. Of course, an abstract is a convenience for the programmer in this case (provided that nothing happens), but adversely affects reuse, clarity and performance (moreso in large code files / programs).

0
source

Source: https://habr.com/ru/post/1286173/


All Articles