Hoping someone can help with this. I keep getting a bad access error when trying to use the -timeIntervalSinceNow
method. I have a variable in this class called NSDate *startDate
and I've added @property (nonatomic, retain) NSDate *startDate;
startDate
is used in the code here:
startDate = [NSDate date];
updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(updatePlaybackPosition:)
userInfo:nil
repeats:YES];
}
}
- (void)updatePlaybackPosition:(NSTimer *)timer {
NSTimeInterval interval = [startDate timeIntervalSinceNow];
When the program reaches [startDate timeIntervalSinceNow]
it gives a bad access error. From the other posts I've read on this topic, the answer usually seems to have something to do with retaining the date. So, I'm not sure what I'm missing. Any help would be much appreciated!
EXC_BAD_ACCESS means that message was sent to a point in the memory where there's no instance of a class to execute it. Thus “bad access”. You will get EXC_BAD_ACCESS in 3 cases: An object is not initialized.
What does EXC_BAD_ACCESS mean? EXC_BAD_ACCESS is an exception raised as a result of accessing bad memory. We're constantly working with pointers to memory in Swift that link to a specific memory address. An application will crash whenever we try to access a pointer that is invalid or no longer exists.
Code Block. Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000000. This indicates that your app crash because it tried to referenced NULL.
Once an Objective-C or Swift object no longer has any strong references to it, the object is deallocated. Attempting to further send messages to the object as if it were still a valid object is a “use after free” issue, with the deallocated object still receiving messages called a zombie object.
Your NSDate
was auto released before the timer fired. updated: Make sure you use the property that you declared instead of the instance variable by using self.
. That will handle the retaining for you properly.
self.startDate = [NSDate date];
and then
- (void)updatePlaybackPosition:(NSTimer *)timer {
NSTimeInterval interval = [self.startDate timeIntervalSinceNow];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With