Home>

Thinking about the rules of iOS memory management

Self-generated generated objects,Hold it yourself.

Non-self-generated objects,I can hold it myself.

Do not release when you need an object you own.

Objects not held by you cannot be released.

Note:Here is the environment used by the object,Understand that the programmer itself is not wrong

Object operations correspond to objective-c methods

Object operation objectivew-c method
Generate and hold objects alloc/copy/mutablecopy/new or methods beginning with it
Object retain
Release object release
Obsolete object dealloc

Self-generated objects,Own

// Generate and hold objects yourself
id obj1=[[nsobject alloc] init];
id obj2=[nsobject new];
id obj3=[obj2 copy];

The copy method is based on the nscopying method convention,Copywithzone in implementation class:

The mutablecopy method is based on the nsmutablecopying method convention,Mutablecopywithzone in the implementation class:

Non-self-generated objects,I can hold it myself

Objects obtained by methods other than alloc/new/copy/mutablecopy,He is not the holder of the object.

// Fetched non-self-generated and held objects,// Get the existence of the object,But he does not own the object.
id obj=[nsmutablearray array];
id obj2=[nsdictionary dictionary];
// own object
[obj retain];
[obj2 retain];

Note:It's a bit hard to understand here,Let's first look at a piece of code:

// Fetched non-self-generated and held objects,// Get the existence of the object,But he does not own the object.
id unretain_obj=[nsmutablearray array];
nslog (@ "unretain_obj retain count =%lu", (unsigned long) [unretain_obj retaincount]);
// call release
[unretain_obj release];

The above code,We print the result:

2016-12-21 15:32:04.485 acm [65216:852108] unretain_obj retain count=1

Subsequent calls to the release method will cause the program to crash!

By reference count,At this point unretain_obj can be executed once the release method. But why does our direct call cause the program to crash.

We will think of one of the four ideas mentioned at the beginning:

Unable to release non-self-owned objects

This way we understand well.Although the retaincount of unretain_obj is printed as 1, it cannot be explained that it refers to the object.It simply acquires the existence of the object.

Then we will have a problem.So who is holding this object??

Let's make a guess first:

Because [nsmutablearray array] is a factory method,The array is definitely going to generate an nsmutablearray instance object.At this time, there must be a pointer to reference it and then return this object.so. . .

Think of it here first,We'll go to verify later

Let's look at another piece of code:

// Fetched non-self-generated and held objects,// Get the existence of the object,But he does not own the object.
id unretain_obj=[nsmutablearray array];
nslog (@ "unretain_obj retain count =%lu", (unsigned long) [unretain_obj retaincount]);
// own object
[unretain_obj retain];
nslog (@ "unretain_obj retain count =%lu", (unsigned long) [unretain_obj retaincount]);
// Release the object you own
[unretain_obj release];
nslog (@ "unretain_obj retain count =%lu", (unsigned long) [unretain_obj retaincount]);

Print results

2016-12-21 15:40:20.774 acm [65682:861135] unretain_obj retain count=1

2016-12-21 15:40:20.774 acm [65682:861135] unretain_obj retain count=2

2016-12-21 15:40:25.254 acm [65682:861135] unretain_obj retain count=1

And the program will not crash.

This also confirms our thoughts above.

Because through the retain method, non-self-generated objects are the same as those generated and held by the alloc/new/copy/mutablecopy method.Become own

Not released when you need an object you own

We know from the above example,The object held by itself calls the release method when it is released, eg:

// Generate and hold objects yourself
id release_obj=[[nsobject alloc] init];
// Release objects held by yourself
[release_obj release];
/ *
 * Release object
 * The pointer to the object is still retained in the variable release_obj, you can still call it.
 * But once the object is released, it is absolutely inaccessible,Otherwise it will cause the program to crash.
 * Exc_bad_access crash
 * /

We implement a method ourselves,Returns a method call that also holds an object,The role of alloc

-(id) allocobject {
   // Generate and hold objects yourself
  id obj=[[nsobject alloc] init];
  // Return an object generated by the alloc method intact
  return obj;

Note:The method name conforms to the rules for generating and holding the object alloc/copy/mutablecopy/new or starting with it

We implement a method ourselves,Returns an object that no one holds,Just get the existence of the object

-(id) object {
  // Generate and hold objects yourself
  id obj=[[nsobject alloc] init];
  // Call the autorelease method to get the existence of the object,But he does not own the object.
  [obj autorelease];
  return obj;

The autorelease method can get the existence of the object,But he does not own the object.Enables the object to be automatically and correctly released when it exceeds the specified survival range (call the release method)

The difference between autorelease and release methods

autorelease:

release:

The detailed explanation of autorelease is introduced later.

We can also make the object of the autorelease method hold the eg by calling the retain method:

// Get the existence of the object,Do not own
 id unretain_obj=[nsmutablearray array];
 // holding object
[unretain_obj retain];

Unable to release non-self-owned objects

I have released and continue to release

 // Generate and hold objects yourself
  id release_obj=[[nsobject alloc] init];
  // Release objects held by yourself
  [release_obj release];
  // Release the released object
  [release_obj release];
  / *
   * Release object
   * The pointer to the object is still retained in the variable release_obj, you can still call it.
   * But once the object is released, it is absolutely inaccessible,Otherwise it will cause the program to crash.
   * Exc_bad_access crash
   * /

Only the existence of the object,Try to release the object

 // Fetched non-self-generated and held objects,  // Get the existence of the object,But he does not own the object.
  id unretain_obj=[nsmutablearray array];
  // Release objects that you do not hold
  [unretain_obj release];

program crash,Report exc_bad_access crash

ios
  • Previous Summary of several cases about the failure of the vector iterator
  • Next An important note about memcpy and memmove
  • Trends