Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXC_BAD_ACCESS error for -timeIntervalSinceNow

Tags:

ios

nsdate

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!

like image 903
cms Avatar asked Aug 10 '11 20:08

cms


People also ask

What does thread 1 Exc_bad_access mean?

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 Xcode?

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.

What is Exc_bad_access Kern_invalid_address?

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.

What is zombie objects in ios?

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.


1 Answers

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];
like image 176
Joe Avatar answered Sep 28 '22 14:09

Joe