本帖最后由 远人 于 2014-4-20 15:06 编辑
- <font color="#000000">#import <Foundation/Foundation.h>
- @interface Person : NSObject
- @property int age;
- - (void)dealloc;
- @end
- #import "Person.h"
- @implementation Person
- - (void)dealloc
- {
- NSLog(@"Person对象被回收了");
- [super dealloc];
- }
- @end
- #import <Foundation/Foundation.h>
- #import "Person.h"
- int main(int argc, const char * argv[])
- {
- Person *p = [[Person alloc] init];
-
- NSUInteger n = [p retainCount];
-
- NSLog(@"%ld",n);
-
- //2
- [p retain];
-
- NSInteger n2 = [p retainCount];
-
- NSLog(@"%ld",n2);
- //1
- [p release];
- //0
- [p release]; //在老师的讲解中,此处Person对象的计数器为0 ,就应该回收Person对象,
-
-
- // [p release];
-
- [p retain];//此处,再进行retain操作就应该报错,这是对野指针进行操作,但是运行不但没有错误,后面检验的计数器显示居然为2,这是什么原因?
-
-
- NSInteger n3 = [p retainCount];
-
- NSLog(@"%ld",n3);
-
- return 0;</font>
复制代码 这段代码是关于野指针的,问题的细节我在代码中又详细询问,这究竟是什么原因?} |