exc_bad_access on [NSDate timeIntervalSinceReferenceDate]

六月ゝ 毕业季﹏ 提交于 2019-12-23 19:00:51

问题


I'm getting weird behavior out of [NSDate timeIntervalSinceReferenceDate].

I have the following function:

-(void) insertRow{
NSTimeInterval timeNow = [NSDate timeIntervalSinceReferenceDate];
if (timeNow - secondsSinceTableViewScroll <0.5){
    [self insertRow];
    return;
}

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:[self.itemsToFollow count] - 1];
[self.tableView insertSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}

which is a callback from an ASIHTTPRequest requestFinished.If I put a breakpoint in the code, it works fine. If I just try to run the code, I get an exc_bad_access on the line:

NSTimeInterval timeNow = [NSDate timeIntervalSinceReferenceDate];

secondsSinceTableViewScroll is an ivar declared in the header and set like this:

 secondsSinceTableViewScroll = [NSDate timeIntervalSinceReferenceDate];

Any ideas why I'm getting the exc_bad_access when there is no breakpoint?

Thanks

Only thing I could find

I was checking the time like this:

-(void) insertRow{
end = [NSDate timeIntervalSinceReferenceDate];
if(end-start < 0.05){
    [self insertRow];
    return;
}

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:[self.itemsToFollow count] - 1];
[self.tableView insertSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}

I'm sure somewhere it says you can't have back to back calls of timeIntervalSinceReferenceDate and that's why it was crashing (this kind of recursive loop was a pretty bad idea anyways).

So use a while loop, or better yet an NSTimer.

Thanks for the help though


回答1:


Change

[self insertRow];

to

[self performSelector:@selector(insertRow) withObject:nil afterDelay:0];



回答2:


Your symptom suggests you're actually crashing on another thread. Do you have other threads running? Check the backtraces for all your threads. The above is on the main thread, correct?


EDIT: You're recursing too quickly and overflowing your stack. Half a second of "as fast as you can call yourself" is going to overflow your stack very quickly.



来源:https://stackoverflow.com/questions/7852865/exc-bad-access-on-nsdate-timeintervalsincereferencedate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!