////Person.h//oc7_ Composite Memory Management (setter method)////Created by zhangxueming on 15/6/18.//Copyright (c) 2015 zhangxueming. All rights reserved.//#import<Foundation/Foundation.h>#import "Dog.h"@interfaceperson:nsobject{Dog*_dog;}-(DOG *) Dog;- (void) Setdog: (Dog *) Dog;@end
////person.m//oc7_ Composite Memory Management (setter method)////Created by zhangxueming on 15/6/18.//Copyright (c) 2015 zhangxueming. All rights reserved.//#import "Person.h"@implementation Person-(DOG *) dog{return_dog;}//method One://-(void) Setdog: (dog *) Dog//{//_dog = dog;//}//Method Two://-(void) Setdog: (dog *) Dog//{//_dog = [dog retain];//}//Method Three:- (void) Setdog: (Dog *) dog//Xiaohei _dog count = 1;{[_dog release];//0//NSLog (@ "Lajfalfjal");//NSLog (@ "JSLFJSLFJL");//NSLog (@ "Lajfalfjal");//NSLog (@ "JSLFJSLFJL");//NSLog (@ "Lajfalfjal");//NSLog (@ "JSLFJSLFJL");//NSLog (@ "Lajfalfjal");//NSLog (@ "JSLFJSLFJL");//NSLog (@ "Lajfalfjal");//NSLog (@ "JSLFJSLFJL");//NSLog (@ "Lajfalfjal");//NSLog (@ "JSLFJSLFJL");//NSLog (@ "Lajfalfjal");//NSLog (@ "JSLFJSLFJL");//NSLog (@ "Lajfalfjal");//NSLog (@ "JSLFJSLFJL");_dog =[dog retain];}//Standard notation//-(void) Setdog: (dog *) Dog//{//if (_dog!=dog) {//[_dog release];//_dog = [dog retain];// }//}- (void) dealloc{[_dog release]; [Super Dealloc];}@end
// // Dog.h// oc7_ Composite Memory Management (setter method)//// Created by Zhangxueming on 15/6/18. // Copyright (c) 2015 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h>@interface dog:nsobject@end
////DOG.M//oc7_ Composite Memory Management (setter method)////Created by zhangxueming on 15/6/18.//Copyright (c) 2015 zhangxueming. All rights reserved.//#import "Dog.h"@implementationDog//-(void) Dealloc//{// //NSLog (@ "12321313");//[Super Dealloc];//}@end
////main.m//oc7_ Composite Memory Management (setter method)////Created by zhangxueming on 15/6/18.//Copyright (c) 2015 zhangxueming. All rights reserved.//#import<Foundation/Foundation.h>#import "Dog.h"#import "Person.h"intMainintargcConst Char*argv[]) {@autoreleasepool {Dog*xiaobai =[[Dog alloc] init]; person*xiaoxin =[[Person alloc] init]; //method One: Not OK, the person does not really hold the dog, if in the main function [Xiaobai release], let dog's reference count minus 1, will become 0,dog destroyed//Xiaoxin.dog = Xiaobai;//NSLog (@ "Retaincount =%li", xiaobai.retaincount);//[Xiaobai release];// //[Xiaoxin release]; //Method Two://not OK, if people hold another dog, it will cause the first dog not to release, memory leaks. //Xiaoxin.dog = Xiaobai;//NSLog (@ "Xiaobai Retaincount =%li", xiaobai.retaincount);//Dog *xiaohei = [[Dog alloc] init];//Xiaoxin.dog = Xiaohei;//[Xiaobai release];//[Xiaohei release];//[Xiaoxin release]; //method Three: Not OK, if it was to hold a dog, and reset the dog, first to release, this time, it is likely that the dog destroyed, and then, can not be retain again. Xiaoxin.dog =Xiaobai; Dog*xiaohei = Xiaobai;//[Xiaobai retain]NSLog (@"Xiaohei retaincount =%li", Xiaobai.retaincount); [Xiaobai release]; NSLog (@"Xiaohei retaincount =%li", Xiaohei.retaincount); Xiaoxin.dog=Xiaohei; [Xiaoxin release]; } return 0;}
OC7_ Composite Memory Management (setter method)