OBJECTIVE-C Learning Notes Memory management

Source: Internet
Author: User

Reference Count

Each object has a multiple integer associated with it, called its reference counter or retention calculator, and when a piece of code needs to access an object, the code adds 1 to the object's reserved counter value, indicating that I want to access the object, and when the code accesses the end, the value of the object's retention counter is reduced by 1. Indicates that the object is no longer being accessed. When the value of the hold counter is 0 o'clock, it means no longer has code access to the object, and it is destroyed while the memory being consumed is reclaimed.

The initial value of the reserved counter value for the object is 1. When an object is about to be destroyed, OC sends an DEALLOC message to the object, which can be overridden in its own object.

Several common methods of invocation:

-(ID) retain;

-(OneWay void) release;

-(Nsuinteger) Retaincount;

Eg:

#import <Foundation/Foundation.h>


@interface Retaintra: nsobject


@end


@implementation Retaintra

-(ID) init

{

if ( self = [super init]) {

NSLog(@ "Init:this is first call--%d.") , [ self retaincount]);

}

return (self);

}

-(void) Dealloc

{

NSLog(@ "Dealloc called. Bye Bye ... " );

[Super dealloc];

}

@end


int Main (int argc, const Char * argv[]) {

Retaintra *tracker = [ retaintra new]; //count:1 The Init is called directly here

[Tracker retain]; //count 2

NSLog (@ "%d--1", [tracker Retaincount]);

[Tracker retain]; //count 3

NSLog (@ "%d--2", [tracker Retaincount]);

[Tracker release]; //count 2

NSLog (@ "%d--3", [tracker Retaincount]);

[Tracker release]; //count 1

NSLog (@ "%d--4", [tracker Retaincount]);

[Tracker release]; Count 0 calls the dealloc.

//nslog (@ "%d--5", [tracker Retaincount]);

return 0;


}


Object ownership

If an object has an instance variable that points to another object, it is said to own the object. Such as. The car object has the engine and tire objects it points to. Similarly, if a function creates an object, it is said that the function owns the object.

Retention and deallocation in Access methods

A better solution to the problem of object release code

-(void) Setengine:(Engine *) newengine

{

[Newengine retain];

[Engine release];

engine = Newengine;

}//setengine

The new engine object is retained first, even if the newengine is the same object as the engine, the value of the reserved counter will be increased first and then reduced immediately. This avoids errors because no 0,engine objects are accidentally destroyed.

In the access method, there is no problem if the new object is retained before the object is released.

Automatic release

Do not know why, see the word, the front has been tense heart suddenly calmed down ...

Today it takes a lot of time to look at reference counts and object ownership, plus the teacher speaks a lot about the basic concepts of functions that need to be remembered slowly.


OBJECTIVE-C Learning Notes Memory management

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.