在释放变量时获取segfault(EXC_BAD_ACCESS) [英] Getting a segfault (EXC_BAD_ACCESS) when deallocating variables

查看:109
本文介绍了在释放变量时获取segfault(EXC_BAD_ACCESS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ok我知道这个错误主要来自发送方法调用或尝试访问已经解除分配的变量。

Ok I understand that this error mostly comes from sending a method call or trying to access a variable that has already been deallocated.

这里是问题: p>

Here is the problem:

.h
@interface TimeEntry : NSObject <NSCopying, NSCoding> {

NSDate *from;
NSDate *to;

NSString *information;


}

@property (nonatomic, retain) NSDate *from;
@property (nonatomic, retain) NSDate *to;

@property (nonatomic, copy) NSString *information;

@end

和我的类的dealloc。

And my classes' dealloc.

-(void)dealloc{

    [super dealloc];    
    [to release];
    [from release];
    [information release];

}

这是traceback的东西,当我得到EXC_BAD_ACCESS错误

This is the traceback thing when I get the EXC_BAD_ACCESS error

所以我发送消息给已经释放的对象?

So I'm sending a message to an object that has been deallocated right?

所以我打开了NSZombie,这个STOPPED我的崩溃。它没有给我一些可爱的崩溃报告,如我所希望的。

So I turned on NSZombie and this STOPPED my crashes. It didn't give me some lovely crash report like I'd hoped. Instead it just kept the program from crashing.

在上面的dealloc方法中,如果我注释掉[释放]和[从发布],应用程序不崩溃。如果我注释掉它们中的一个..它不会崩溃。在调试窗口中,来自和有不同的内存地址。

In the dealloc method above if I comment out [to release] and [from release] the app doesnt crash. If I comment out just one of them.. it doesn't crash. In the debug window to and from have different memory addresses.

内存管理如何这么难!!!!

How can memory management be so hard!!!!

有任何线索吗?

感谢,

Dan

推荐答案

在释放变量后发送[super dealloc]消息,而不是之前。 [super dealloc]应该是你在dealloc方法中最后做的事情。

Send the [super dealloc] message after you've released your variables, not before. [super dealloc] should be the last thing you do in your dealloc method.

这篇关于在释放变量时获取segfault(EXC_BAD_ACCESS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆