(转)self的内存分析

1、加self的方式: 

1 Student *mystudent = [[Student alloc] init];       //mystudent 对象 retainCount = 1;
2 self.student = mystudent;   //student 对象 retainCount = 2;
3 [mystudent release];  //student 对象 retainCount = 1;

retainCount指对象引用计数,student的property 是retain 默认使用self.student引用计数+1。

2、不加self的方式

1 Student *mystudent = [[Student alloc] init];       //mystudent 对象 retainCount = 1;
2 student = mystudent;   //student 对象 retainCount = 1;
3 [mystudent release];   //student 对象内存已释放,如果调用,会有异常

3、加self直接赋值方式

 

1 self.student = [[Student alloc] init];  //student 对象 retainCount = 2;容易造成内存泄露

由于objective-c内存管理是根据引用计数处理的,当一个对象的引用计数为零时,gcc才会释放该内存

posted @ 2012-06-14 13:12  dsqtb  阅读(127)  评论(0)    收藏  举报