timeIntervalSinceDate 给出 exc_bad_access 或 autorelease 错误
所以我有一个对象代表 iPhone 屏幕上绘制的一条线。我还有一个球在屏幕上移动,当球和线相交时,线就被赋予了生命。对于我当前的构建,我不断收到此错误,没有崩溃,但球停止在屏幕上移动:
2011-08-18 11:00:05.436 myProgram[192:5e03] * __NSAutoreleaseNoPool( ): NSCFString 类的对象 0x1531c0 自动释放,没有池 - 只是泄漏
Line.h: @interface Line : NSObject {
//time properties
NSTimeInterval life;
NSDate *startTime;
NSDate *currentTime;
}
//time properties
@property (nonatomic, retain) NSDate *startTime;
-(void) updateLife;
-(void) beginLifeTracking;
@end
Line.m
@synthesize startTime;
-(void)beginLifeTracking {
[self.startTime release]; //not sure if self is necessary here but startTime is released
//in case the same string is hit again
self.startTime = [NSDate date];
startTime = [NSDate dateWithTimeIntervalSince1970:0];
//NSLog(@"Time Interval: %f",startTime);
}
-(void) updateLife {
currentTime = [[NSDate date] retain];
NSLog(@"breakpoint1");
life = [currentTime timeIntervalSinceDate:self.startTime];
[currentTime release];
}
我假设这是某种内存管理错误,但我所有的补救尝试都失败了。我真的很感激对我在这里做错了什么的解释。谢谢!
So I have an object which represents a line drawn on the iPhone screen. I also have a ball moving around on the screen, and when the ball and the line intersect, the string is given a life. For my current build I am receiving this error constantly, with no crashes, but the ball stops moving on the screen:
2011-08-18 11:00:05.436 myProgram[192:5e03] * __NSAutoreleaseNoPool(): Object 0x1531c0 of class NSCFString autoreleased with no pool in place - just leaking
Line.h:
@interface Line : NSObject {
//time properties
NSTimeInterval life;
NSDate *startTime;
NSDate *currentTime;
}
//time properties
@property (nonatomic, retain) NSDate *startTime;
-(void) updateLife;
-(void) beginLifeTracking;
@end
Line.m
@synthesize startTime;
-(void)beginLifeTracking {
[self.startTime release]; //not sure if self is necessary here but startTime is released
//in case the same string is hit again
self.startTime = [NSDate date];
startTime = [NSDate dateWithTimeIntervalSince1970:0];
//NSLog(@"Time Interval: %f",startTime);
}
-(void) updateLife {
currentTime = [[NSDate date] retain];
NSLog(@"breakpoint1");
life = [currentTime timeIntervalSinceDate:self.startTime];
[currentTime release];
}
I'm assuming this is some sort of memory management error, but all my attempts to remedy it have failed. I'd really appreciate an explanation of what I'm doing wrong here. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您调用
[self.startTime release]
时,您就过度释放了。执行此释放是访问器 (setStartTime:
) 的工作。该代码没有任何意义,并且很危险,因为它留下了一个悬空的 ivar:
这应该是:
您收到 autoreleasepool 错误的事实表明您正在后台线程上运行它。您的代码不是线程安全的,因此这将是一个问题。
编辑关于线程代码,您如何调用
beginLifeTracking
?那就是我怀疑你进入错误线程的地方。我会非常关心这个 autoreleasepool 警告。When you call
[self.startTime release]
, you're over-releasing. It's the job of the accessor (setStartTime:
) to do this release.The code doesn't make any sense, and is dangerous since it leaves a dangling ivar:
This should be:
The fact that you're getting an autoreleasepool error suggests you're running this on a background thread. Your code is not thread-safe, so that would be a problem.
EDIT Regarding threading code, how are you calling
beginLifeTracking
? That would be where I would suspect you're getting onto the wrong thread. I would be very concerned about this autoreleasepool warning.此行:
将使用自动释放的
NSDate
覆盖实例变量startDate
。方法退出后不久,该实例将被释放并成为悬空指针。这就是导致你崩溃的原因。
您的方法可能应该如下所示:
我还取消了您的日志语句的注释并使其正常工作。
This line:
Will overwrite the instance variable
startDate
with an autoreleasedNSDate
. Menaing the instance will be released and become a dangling pointer soon after the method has exited.This is what causes your crash.
You method should probably look like this:
I also uncommented your log statement and made it work.