MRC模式下,管理对象内存的3原则为:
①、OC里不允许重载运算符,因此,当使用 “=” 运算符的时候,并不能通过这个运算符的重载函数来维持对象的retainCount数据,必须在本类的方法中显式地调用 对象字段的 retain方法,否则,可能对象字段的实际内存已经被系统回收了,本类的对象还不知道,继续使用这个对象,造成严重的错误。 代码如下:
//下面代码实在MRC模式下运行的
#import <Foundation/Foundation.h>
@interface person:NSObject
{
NSString *name;
}
@property NSString *name;
+(instancetype) newPersonWithName: (NSString *) strName;
@end
@implementation person :NSObject
@synthesize name;
+(instancetype) newPersonWithName: (NSString *) strName
{
person* newSelf= [self new];
newSelf.name=strName;
return newSelf;
}
@end
int main(int argc,const char * argv[])
{
NSMutableString *mstr=[NSMutableString stringWithString:@"liji"];
printf("%lu\n",[mstr retainCount]); //1
person *perX=[person newPersonWithName:mstr];
printf("%lu\n",[mstr retainCount]); //1
[mstr release];
printf("%lu\n",[mstr retainCount]); //输出非常大的数据,但是没有抛异常,程序出错
}
②、修改上面的代码,在person类的newPersonWithName方法中,加上其形参的retain方法调用,程序如下。
执行程序,可以看到,又有新的错误。
perX在执行了release后,其字段name引用的对象 mstr所占用的内存没有被释放掉。
#import <Foundation/Foundation.h>
@interface person:NSObject
{
NSString *name;
}
@property NSString *name;
+(instancetype) newPersonWithName: (NSString *) strName;
@end
@implementation person :NSObject
@synthesize name;
+(instancetype) newPersonWithName: (NSString *) strName
{
person* newSelf= [self new];
newSelf.name=strName;
[strName retain];
return newSelf;
}
@end
int main(int argc,const char * argv[])
{
NSMutableString *mstr=[NSMutableString stringWithString:@"liji"];
printf("%lu\n",[mstr retainCount]); //1
person *perX=[person newPersonWithName:mstr];
printf("%lu\n",[mstr retainCount]); //2
[mstr release];
printf("%lu\n",[mstr retainCount]);
[perX release]; //1
printf("%lu\n",[mstr retainCount]); //1
}
③、再次修改上面的代码, person类重新定义release方法,方法里释放掉 name 对象 ,可以看到,程序正常运行了
#import <Foundation/Foundation.h>
@interface person:NSObject
{
NSString *name;
}
@property (assign) NSString *name;
+(instancetype) newPersonWithName: (NSString *) strName;
-(void)release;
@end
@implementation person :NSObject
@synthesize name;
+(instancetype) newPersonWithName: (NSString *) strName
{
person* newSelf= [self new];
newSelf.name=strName;
[strName retain];
return newSelf;
}
-(void)release
{
[name release];
[super release];
}
@end
int main(int argc,const char * argv[])
{
NSMutableString *mstr=[NSMutableString stringWithString:@"liji"];
printf("%lu\n",[mstr retainCount]); //1
person *perX=[person newPersonWithName:mstr];
printf("%lu\n",[mstr retainCount]); //2
[mstr release];
printf("%lu\n",[mstr retainCount]);
[perX release]; //1
printf("%lu\n",[mstr retainCount]); //这里抛异常了,说明前面添加的person类的release方法有效
}