IPhone Development Notes [12/50]: Memory leakage is a pain point for beginners. nsmutablearray is used correctly.

Source: Internet
Author: User

IPhoneProgramMemory leakage during development is a headache for new users. It may be that the C # language is too familiar to use, I used the profile tool in xcode to check the small program I wrote, and the memory leaked a lot. after a while of troubleshooting, adding an object to nsmutablearray does not properly maintain the reference count of the object.

In the topic nsmutablearray how to properly addobjects and release, the correct steps for using nsmutablearray are given first.Code.

Nsmutablearray * listdata = [[nsmutablearray alloc] init];For(IntI = 0; I <3; I ++) {mydata * OBJ = [[mydata alloc] init]; nsstring * name = nil;Switch(I ){Case0: Name =@ "Semen";Break;Case1: Name =@ "Ivan";Break;Case2: Name =@ "Stepan";Break;Default:Break;} Obj. Name = Name; [listdata addobject: OBJ];

[OBJ release];

} [Listdata release];

Three problems are mentioned in this Article. After translation, I will add my understanding:

(1) What are the objects in the nsmutablearray array? Is a copy of an object, or is it just a pointer to an object?

Answer: What is stored in the array is not a copy of the object, but a pointer to the object.

According to the previous C ++ thinking method, the above [OBJ release] is the most difficult to understand. I put OBJ in an array, which stores object references, why is OBJ released? In this way, an invalid pointer is stored in the array? In fact, the way of thinking has not changed. In objective-C, [OBJ release] only indicates that obj receives a release message. If its reference count does not change to 0, it will not be released, when we see this release in C ++, we think of release. Let's look at a process:

Mydata * OBJ = [[mydata alloc] init]; // OBJ uses the init method. According to the Convention, the reference count of obj is 1, and the release process must be maintained by itself.

[Listdata addobject: OBJ]; // when put in the array, OBJ automatically adds 1 to the reference count of obj. In this case, the reference count of obj is 2.

[OBJ release]; // in order to maintain the normal Count value of OBJ, use this statement to set the reference count of OBJ to 1. That's all. It is not released!

[Listdata release]; // This statement will send a release message to OBJ, so that the reference count of obj is changed to 0 and destroyed. If the previous statement [OBJ release] is not written, the OBJ object is not released normally, causing memory leakage!

(2) Do I need to release all objects in the array before releasing the nsmutablearray object?

A: No.

In the release nsmutablearray object, it automatically sends a release message to the object.

(3) What are the steps to correctly use nsmutablearray? (Alloc, init, work, release)

A:

1. nsmutablearray * arr = [[nsmutablearray alloc] init]; // assign an array

2. alloc object1. // allocate obj1

3. Add object1 to array. // Add obj1 to the array.

4.Release object1.// The obj1 reference count minus 1

5. alloc object2. // allocate obj2

6. Add object2 to array. // Add obj2 to the array.

7.Release object2.// The obj2 reference count minus 1

8. Add as jsonobjects as needed in this manner. // according to the above method, you can add more objects.

8. Work with object1. // you can access objects in it.

9. Remove object1 from array. It will receive a release automatically. // You can also remove obj1. In this case, obj1 will automatically receive a release message.

10. [arr release]; // object2 and others will receive a release. Finally, the array is released, and all elements in the array will automatically get a release message.

 

The above principle applies to the setobject method of the nsmutabledictionary class.

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.