malloc 错误和模拟器崩溃

发布于 2024-09-28 12:06:05 字数 1089 浏览 4 评论 0原文

我不断收到 malloc 错误:双重释放......

echoTime 函数正在由按钮调用。当我在计时器结束之前再次按下按钮时,会出现 malloc 错误。当我在计时器完成后按下按钮再次启动时,模拟器挂起。

有谁知道下面这段代码有什么问题:

-(IBAction)echoTime: (id) sender {
    if (gameTimer != nil) {
        [gameTimer invalidate];
        [gameTimer release];
    }
    NSInteger secs = 1 * 60;
    if (secs != 0) {
        NSNumber *elapsedSeconds = [[NSNumber alloc] initWithInt:secs];
        NSDictionary *myDict = [NSDictionary dictionaryWithObject:elapsedSeconds forKey:@"TotalSeconds"];
        gameTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(echoIt:) userInfo:myDict repeats: YES];
    }
}

-(void)echoIt: (NSTimer *) timer {
    NSNumber *num = (NSNumber *) [[timer userInfo] valueForKey:@"TotalSeconds"];
    seconds++;
    NSInteger sec = [num integerValue] - seconds;
    NSInteger minutes = sec / 60;
    [gameTimeLabel setText:[NSString stringWithFormat:@"%02i:%02i", minutes, sec-(60*minutes)]];
    if (sec == 0) {
        [self playSound:@"Horn"];
        [gameTimer invalidate];
    }
}

I constantly get malloc error : double freed....

The echoTime function is being called by a button. When I press the button again before the timer ends it gives me the malloc error. When I press the button after the timer finished to start it again, the simulator hangs.

Does anyone know what is wrong with the following piece of code:

-(IBAction)echoTime: (id) sender {
    if (gameTimer != nil) {
        [gameTimer invalidate];
        [gameTimer release];
    }
    NSInteger secs = 1 * 60;
    if (secs != 0) {
        NSNumber *elapsedSeconds = [[NSNumber alloc] initWithInt:secs];
        NSDictionary *myDict = [NSDictionary dictionaryWithObject:elapsedSeconds forKey:@"TotalSeconds"];
        gameTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(echoIt:) userInfo:myDict repeats: YES];
    }
}

-(void)echoIt: (NSTimer *) timer {
    NSNumber *num = (NSNumber *) [[timer userInfo] valueForKey:@"TotalSeconds"];
    seconds++;
    NSInteger sec = [num integerValue] - seconds;
    NSInteger minutes = sec / 60;
    [gameTimeLabel setText:[NSString stringWithFormat:@"%02i:%02i", minutes, sec-(60*minutes)]];
    if (sec == 0) {
        [self playSound:@"Horn"];
        [gameTimer invalidate];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

隔纱相望 2024-10-05 12:06:05

NSNumber *elapsedSeconds = [[NSNumber alloc] initWithInt:secs];

一般来说,不要分配 NSNumbers 和 NSDictionary(和 NSArray、NSSet)总是保留它所给定的对象(并在应该的时候释放它们) )。

尝试...

NSNumber *elapsedSeconds = [NSNumber numberWithInt:secs];

它会停止您拥有的保留周期,并可能会停止崩溃。

也无关紧要但有风格。

NSInteger secs = [[[timer userInfo] valueForKey:@"TotalSeconds"] intValue];

效率更高一些。

NSNumber *elapsedSeconds = [[NSNumber alloc] initWithInt:secs];

In general dont alloc NSNumbers and a NSDictionary (and NSArray,NSSet) always retains the objects it is given (and releases them too when it should).

try...

NSNumber *elapsedSeconds = [NSNumber numberWithInt:secs];

It'll stop the retain cycle you've got and might stop the crash.

Also irrelevant but stylewise.

NSInteger secs = [[[timer userInfo] valueForKey:@"TotalSeconds"] intValue];

Is a little more efficient.

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