首页 > 代码库 > 黑马程序员 内存管理之proterty,autorelease
黑马程序员 内存管理之proterty,autorelease
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
Book * _book;
int _age;
}
/*
oc对象
1.@property能制动生成setter和getter方法
2.retain告诉编译器自动生成setter里的retain
3.atomic:低性能(默认情况下)
nonatomic:高性能
*/
@property (nonatomic, retain) Book *book;
/*
非oc对象
1.@property能制动生成setter和getter方法
2.assign告诉编译器自动生成setter的assign设置
*/
@property (nonatomic, assign) int age;
@end
@implementation Person
- (void)dealloc
{
[_book release];
[super dealloc];
}
@end
@interface Book : NSObject
@end
@implementaton Book
@end
int main()
{
/*
1.当调用autorelease方法,会返回对象本身,对象计数器不做任何改变
2.autorelease会把对象放在自动释放池里
3.当自动释放池被销毁时,对释放池里的所有对象进行一次release
4.所有的释放池都是放在内存栈中,采取先进后出原则,把所有的对象放在栈顶
*/
@autoreleasepool
{// 创建自动释放池
Person *P = [[[Person alloc] init] autorelease];
@autoreleasepool
{
Book *b = [[[Book alloc] init] autorelease];
}
}// 销毁自动释放池
return 0;
}
void test()
{
Person *p = [[Person alloc] init];
Book *b1 = [[Book alloc] init];
p.book = b1;
Book *b2 = [[Book alloc] init];
p.book = b2;
[b2 release];
[b1 release];
[p release];
}