Memory management analysis for multiple objects

Source: Internet
Author: User

//

Main.m

03-Memory management analysis for multiple objects

//

Created by Apple on 14-3-17.

//

//

#import <Foundation/Foundation.h>

People have a car

#import "Person.h"

1. Wild pointer problem

2. Memory leak issues

int main (int argc, const char * argv[])

{

@autoreleasepool {

1

person * p = [[Person alloc] init];

1

Car * car = [[Car alloc] init];

2

[P Setcar:car];

[P Drive];

1

[Car release];

To ensure that the object P exists, any method that invokes the object p does not go wrong

[P Drive];

0

[P release];

}

return 0;

}

//

Person.m

01-Manual memory management Basic concepts

//

Created by Apple on 14-3-17.

//

//

#import "Person.h"

@implementation person

-(void) Setcar: (Car *) car

{

//2, able to make Retaincount + 1 and return to self

_car = [car retain];

}

-(CAR *) car

{

return _car;

}

-(void) drive

{

[_car Run];

}

-(void) dealloc

{

The goal is to ensure that when the P object exists, the car object must exist

When the object P is destroyed,

[_car release];

[Super Dealloc];

NSLog (@ "person has been destroyed");

}

@end

Memory management analysis for multiple objects

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.