NSObject released after layoutSubviews
First I have a class inherited from NSObject
@interface Role : NSObject{ /*...*/ } @end
And there is a property to store the pointer of the instant for Role
class.
@property (nonatomic, retain) Role * role;
Now when the UIView
created, I initialize an instant of the Role
class.
Role * r = [[Role alloc] init];
role = r;
[r release];
As I understand, the property role
is开发者_运维百科 marked with retian
, then it will automatically increase the reference count when I set the value, and decrease the reference count when the property is set to nil when the application exit.
Everything looks fine but when I run the application, I found that I will get EXC_BAD_ACCESS exception.
I added NSLog
and this is because the Role
instant has been released
and dealloced
after the UIView
call layoutSubviews
, I can't understand why this happen because I DO NOT have any code to release
this instant.
Current temporarily solution for me is: I comments out the line [r release]
Please can someone give me some explanation if there is some background I don't know for layoutSubviews
?
Thanks
You are accessing your ivar directly. This should have been:
self.role = r;
Avoid accessing your ivars directly; always use accessors except in init
and dealloc
.
You should actually call the property "setter method" to retain the value.
self.role = r;
or
[self setRole:r];
精彩评论