OBJ-C Memory Management

Source: Internet
Author: User
OBJ-C provides an internal counter for each object to track the number of references of the object. The increase and decrease of the number of references correspond to the retain and release methods of nsobject respectively. When the number of references is 0, the object will be recycled immediately. The alloc and copy methods of the object will send a retain message, and the reference count will be incremented by 1. "Who gave birth to the child, who should raise it? "override the dealloc method. When an object contains other objects, You have to release them in the dealloc method-(void) dealloc {[myvar release]; ...... [Super release];} the memory automatic release pool autorelease Pool provides an object container. When an object sends an autorelease message, the reference count of the object is not reduced, the memory Auto Release pool will add a corresponding record to record this requirement on objects. When the memory Auto Release pool sends a release message, it will notify all objects in the pool to send a release message, the reference count will actually reduce the template code nutoreleasepool * Pool = [[nutoreleasepool alloc] init]; [myobject autorelease]; ...... [pool release]; Memory Management of properties setting the value of the member variable in the property and constructor can easily cause memory leakage/error-(void) settitle :( nsstring *) newtitle {// the memory of the old title is not released. // This is just a pointer value, which can easily cause problems: Title = newtitle ;} // correct syntax (equivalent to the retain parameter in the property parameter)-(void) settitle :( nsstring *) newtitle {// ensure that the uploaded newtitle will not be release [newtitle retain]; // release the old title [Title release]; // create a new Title = [[nsstring alloc] initwithstring: newtitle]; [newtitle release];} // correct syntax (equivalent to the copy parameter in the property parameter)-(void) settitle :( nsstring *) newtitle {// ensure that the uploaded newtitle will not be release [newtitle copy]; // only sets up the object that implements the nscopying protocol // releases the old title [Title release]; // creates a new Title = [[nsstring alloc] initwithstring: newtitle]; [newtitle release];} The assign parameter applies to basic types of direct value assignment-(void) setage :( INT) newage {age = newage;} constructor memory management-(ID) initwithtitle :( nsstring *) newtitle {self = [Super init]; If (Self) {[self settitle: newtitle]; // This write method may cause memory leakage and pointer problems, is the incorrect syntax // self-> Title = newtitle ;}}
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.