OC7_复合类内存管理(setter方法)

//
// Person.h
// OC7_复合类内存管理(setter方法)
//
// Created by zhangxueming on 15/6/18.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Dog.h" @interface Person : NSObject
{
Dog *_dog;
} - (Dog *)dog; - (void)setDog:(Dog *)dog; @end
//
// Person.m
// OC7_复合类内存管理(setter方法)
//
// Created by zhangxueming on 15/6/18.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "Person.h" @implementation Person - (Dog *)dog
{
return _dog;
} //方法一:
//- (void)setDog:(Dog *)dog
//{
// _dog = dog;
//} //方法二: //- (void)setDog:(Dog *)dog
//{
// _dog = [dog retain];
//} //方法三: - (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];
} //标准写法
//- (void)setDog:(Dog *)dog
//{
// if (_dog!=dog) {
// [_dog release];
// _dog = [dog retain];
// }
//} - (void)dealloc
{
[_dog release];
[super dealloc];
} @end
//
// Dog.h
// OC7_复合类内存管理(setter方法)
//
// 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_复合类内存管理(setter方法)
//
// Created by zhangxueming on 15/6/18.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "Dog.h" @implementation Dog
//-(void)dealloc
//{
// //NSLog(@"12321313");
// [super dealloc];
//}
@end
//
// main.m
// OC7_复合类内存管理(setter方法)
//
// Created by zhangxueming on 15/6/18.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Dog.h"
#import "Person.h" int main(int argc, const char * argv[]) {
@autoreleasepool {
Dog *xiaoBai = [[Dog alloc] init];
Person *xiaoXin = [[Person alloc] init];
//方法一: 不ok,人并没有真正持有狗,如果在main函数里[xiaoBai release],让dog的引用计数减1,就变为0,dog就销毁了
// xiaoXin.dog = xiaoBai;
// NSLog(@"retainCount = %li", xiaoBai.retainCount);
// [xiaoBai release];//
// [xiaoXin release];
//方法二:
//不ok,如果人再持有别的狗,就会造成第一条狗得不到释放,内存泄露。
// xiaoXin.dog = xiaoBai;
// NSLog(@"xiaoBai retainCount = %li", xiaoBai.retainCount);
// Dog *xiaoHei = [[Dog alloc] init];
// xiaoXin.dog = xiaoHei;
// [xiaoBai release];
// [xiaoHei release];
// [xiaoXin release];
//方法三:不ok,如果本来持有一条狗,又重新设置这条狗,先进行release,这个时候,很可能dog就销毁了,然后,就没法再次retain了。
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 ;
}
上一篇:为什么不能在SSH(Secure Shell)登录中设置ulimit值?


下一篇:WebService大讲堂之Axis2(10):使用soapmonitor模块监视soap请求与响