timeIntervalSinceDate 给出 exc_bad_access 或 autorelease 错误

发布于 2024-11-30 09:46:53 字数 1155 浏览 0 评论 0原文

所以我有一个对象代表 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聚集的泪 2024-12-07 09:46:53

当您调用[self.startTime release]时,您就过度释放了。执行此释放是访问器 (setStartTime:) 的工作。

该代码没有任何意义,并且很危险,因为它留下了一个悬空的 ivar:

currentTime = [[NSDate date] retain];
NSLog(@"breakpoint1");   
life = [currentTime timeIntervalSinceDate:self.startTime];
[currentTime release];

这应该是:

self.life = [[NSDate date] timeIntervalSinceDate:self.startTime];

您收到 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:

currentTime = [[NSDate date] retain];
NSLog(@"breakpoint1");   
life = [currentTime timeIntervalSinceDate:self.startTime];
[currentTime release];

This should be:

self.life = [[NSDate date] timeIntervalSinceDate:self.startTime];

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.

倾`听者〃 2024-12-07 09:46:53

此行:

startTime = [NSDate dateWithTimeIntervalSince1970:0];

将使用自动释放的 NSDate 覆盖实例变量 startDate。方法退出后不久,该实例将被释放并成为悬空指针。

这就是导致你崩溃的原因。

您的方法可能应该如下所示:

-(void)beginLifeTracking {
    self.startTime = [NSDate dateWithTimeIntervalSince1970:0];
    NSLog(@"Time Interval: %@", self.startTime);
}

我还取消了您的日志语句的注释并使其正常工作。

This line:

startTime = [NSDate dateWithTimeIntervalSince1970:0];

Will overwrite the instance variable startDate with an autoreleased NSDate. 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:

-(void)beginLifeTracking {
    self.startTime = [NSDate dateWithTimeIntervalSince1970:0];
    NSLog(@"Time Interval: %@", self.startTime);
}

I also uncommented your log statement and made it work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文