Basic knowledge of IOS Memory Management

Source: Internet
Author: User

First, let's briefly explain the functions of nil and release: Nil sets the pointer of an object to null, but disconnects the pointer from the object in memory; the release actually notifies the memory to release this object. Therefore, nil does not release the memory. Only release can actually release the memory. After an object is release, the memory allocated to it has been released. if the object is used by the system after it is released, the program will crash. If the pointer is set to null after the object is released, the system will not crash even if the subsequent program uses the object. If no release exists, the object is directly set to nil, and retaincount is equal to 0, causing memory leakage.

Class member variable, which defines the property as retain. When assigning values to this member variable, you must reference self. Call this variable. However, do not reference self. In alloc to assign values to this variable.

For example, self. m_strtest = [[nsstring alloc] init]; is incorrect. The correct one is m_strtest.
= [[Nsstring alloc] init];

When initializing an object using the class method, the retaincount of the object is 1.

For example

Searchbutton = [uibuttonbuttonwithtype: uibuttontypecustom];

In this case, the retaincount of searchbutton is 1, but you do not need to release it yourself.


When you add an object to the array. What is stored in the array is not a copy of the object, but a pointer to the object. Saving the Array
This pointer also sends a retain message to the object indicated by the pointer. Correspondingly, the hold count of the object increases. When the object is removed from the array, the release message is also sent to the object, and the holding count of the object is reduced. When the array is released, the release message is sent to all objects in the array. Let's look at the two examples below:

1. No memory version is released
Array = [[nsmutablearray alloc] init];
For (I = 0; I <10; I ++ ){
Newnumber = [[nsnumber alloc] initwithint :( I * 3)];
[Array addobject: newnumber];
}

When creating a newnumber object, the code above sends a retain message to the object, and the hold count of the object changes to 1. When the reference of this object is added to the array, a retain message is sent to the object, so that the object's Hold count is changed to 2. When array is used up, we will habitually release the array, but this will not release
And only changes the Hold count of all objects to 1. These objects still occupy the memory.

2. Release the memory version
For (I = 0; I <10; I ++ ){
Newnumber = [[nsnumber alloc] initwithint :( I * 3)];
[Array addobject: newnumber];
[Newnumber release];
}


Nsstring * test = [[nsstringalloc] initwithformat: @ "% @", @ "hello"];

Nslog (@ "after alloc = % d", [testretaincount]); // 1

[Test retain]; // 2

Nsstring * Test2 = test;

[Test2 retain];

Nslog (@ "after retain = % d", [test retaincount]); // 3

Nsmutablearray * array = [[nsmutablearrayalloc] init];

[Array addobject: Test];

Nslog (@ "after retain = % d", [testretaincount]); // 4

[Array removeobjectatindex: 0];

Nslog (@ "after array removeobjectatindex = % d", [test retaincount]); // 5

// [Array removeallobjects];

// Nslog (@ "after array removeallobjects = % d", [testretaincount]); // 5

If (1 = [Test
Retaincount]) // release and nil should be written in this way, so as to prevent memory leakage during misuse of nil, and prevent this object from being used in crash after release.

{

[Test release];

Test = nil;

}

Else

{

[Test release];

}

If (test)

{

Nslog (test );

}

Nsstring * test = [[nsstringalloc] initwithformat: @ "% @", @ "hi"];

[Test release];

[Test release]; // error (message sent to deallocated instance) Because test has been release.

Related Article

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.