Using NSDate as a property gives me EXC_BAD_ACCESS, why?
I am adding NSDate as a pointer with a property, and every time I unload my view, it crashes with EXC_BAD_ACCESS. I am doing (not posting full code):
.h
NSDate *scheduledDate;
@property (nonatomic, retain) NSDate *scheduledDate;
.m
@synthesize scheduledDate;
- (void)dealloc {
[super dealloc];
[asset release];
[passedDate release];
[eventDate release];
[eventName release];
}
I have not done anything else with the pointer, but I still get EXC_BAD_ACCESS. Why is this happening? Is there a different way to set the property for NSDate?
SORRY:
I fixed an error in开发者_StackOverflow my question code, it was only a copy and paste issue, not a fix to my problem, it still exists.
You're calling [super dealloc] before the release in your dealloc implementation. That means the [scheduledDate release] is release some non-free memory (which is no longer nil).
Specifically, change the order so [super dealloc] is last:
- (void)dealloc {
[asset release];
[passedDate release];
[eventDate release];
[eventName release];
[super dealloc];
}
Your code looks inconsistent. The @property declaration should be for "NSDate scheduledDate", not "NSString ...".
If you are not using @property (nonatomic, retain) NSDate *scheduledDate;
then there is no need to synthesize scheduledDate
.
Also, how are you determining the value of scheduledDate
? post the code you are using for it.
Also, post the crash log that your app produces.
Your @property (nonatomic, retain)
is for "passedDate", not "scheduledDate". Since "scheduledDate" is not getting instantiated and retained by @property (nonatomic, retain)
, then attempting to release it is attempting to release something that hasn't been created yet (it isn't being synthesized as a property).
精彩评论