标签:
#import <Foundation/Foundation.h> #import "Person.h" int main(int argc, const char * argv[]) { // @autoreleasepool { // // Person *p = [[Person alloc] initWithName:@"池" gender:@"nan" age:18]; // NSLog(@"%lu",[p retainCount]); // // // NSLog(@"--------------"); // Person *p1 = [p retain]; // NSLog(@"%lu",[p retainCount]); // NSLog(@"%lu",[p1 retainCount]); // // NSLog(@"--------------"); // // Person *p2 = [p1 retain]; // NSLog(@"%lu",[p retainCount]); // NSLog(@"%lu",[p1 retainCount]); // NSLog(@"%lu",[p2 retainCount]); // // NSLog(@"--------------"); // //release 让对象的引用计数立即 -1 // [p release]; // NSLog(@"%lu", [p retainCount]); // [p release]; // NSLog(@"%lu", [p retainCount]); // [p release]; // NSLog(@"%lu", [p retainCount]);// 错误的 // //谁才有引用计数的概念: // //1.堆区空间才有引用计数的概念 // //2.堆区的对象才会有引用计数 // // } // Person *p = [[Person alloc] initWithName:@"张三" gender:@"男" age:18]; // NSLog(@"%lu", [p retainCount]); // [p retain]; // NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // // // // [p autorelease]; // // NSLog(@"%lu",[p retainCount]); // // // // [pool release]; // NSLog(@"%lu",[p retainCount]); // //企业级题 // @autoreleasepool { // for (long i = 0; i < 100000000; i++) { // @autoreleasepool { // Person *per = [[Person alloc] init]; // [per autorelease]; // } // } // } //内存管理原则:当你对一个对象进行了 alloc, retain, copy 操作,你就对该对象拥有了所有权,要对该对象负责,对其进行release,或者autorelease操作 //在一个方法内部,要保证对象的+ 和 - 相等. //创建Person对象 @autoreleasepool { Person *p = [Person personWithName:@"张三" gender:@"男" age:44]; Person *p1 = [p copy]; [p1 sayHi]; [p1 release]; } //内存问题: //1.野指针错误:最后一个release玩,空间已经被系统回收,如果再访问,就是访问一块没有所有权的空间,出现野指针,野指针不会立即报错,但是存在安全隐患,另外,错误不容易排错. //2.过度释放:对一块没有所有权的空间进行释放空间,会造成程序的立即crash; //3.内存泄露:对一块拥有所有权的空间,使用完之后没有释放,此时存在安全隐患,当内存泄露足够多得时候,造成程序的crash; return 0; }
person.h
#import <Foundation/Foundation.h> @interface Person : NSObject<NSCopying> @property (nonatomic,retain) NSString *name; @property (nonatomic,retain) NSString *gender; @property (nonatomic,assign) NSInteger age; - (instancetype)initWithName:(NSString *)name gender:(NSString *)gender age:(NSInteger)age; + (instancetype)personWithName:(NSString *)name gender:(NSString *)gender age:(NSInteger)age; - (void)sayHi; @end
person.m
#import "Person.h" @implementation Person - (instancetype)initWithName:(NSString *)name gender:(NSString *)gender age:(NSInteger)age { if (self = [super init]) { self.name = name; self.gender = gender; self.age = age; } return self; } + (instancetype)personWithName:(NSString *)name gender:(NSString *)gender age:(NSInteger)age{ return [[[self alloc] initWithName:name gender:gender age:age] autorelease]; } - (void)sayHi { NSLog(@"sayHigoggo"); } //当对一个兑现copy时,会自动调用该方法,用来创建新的对象,复制内容 - (id)copyWithZone:(NSZone *)zone { //1.创建新的对象 Person *newPer = [[Person allocWithZone:zone] init]; //2.拷贝内容 newPer.name = self.name; newPer.age = self.age; newPer.gender = self.gender; //3.返回新的对象 return newPer; } //当对象的引用计数为0是系统会自动调用,切记不要人为调用 - (void)dealloc { NSLog(@"%@回收了",self); [super dealloc]; } @end
标签:
原文地址:http://www.cnblogs.com/wohaoxue/p/4725506.html