对于使用new创建的类对象,使用delete释放内存. [英] freeing the memory using delete for a class object created using new gives exception.

查看:85
本文介绍了对于使用new创建的类对象,使用delete释放内存.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用delete为使用new创建的类对象释放内存释放异常.
这个班是

freeing the memory using delete for a class object created using new gives exception.
The class is

class CSnp : public CObject
{
    public:
        CSnp();
        ~CSnp();
        char liststr[50];
        char sSnpDesc[50];
        CTime tDateTime;
        int iDCSSerialKey;        
 };

推荐答案

删除异常通常表示您在使用过程中超出了分配范围...在运行时使用
an exception on delete normally means you''ve exceeded the bounds of the allocation during use ... i''d look at the use of
liststr 

sSnpDesc 


在您的析构函数中放置一个断点.产生异常的主要原因是由于错误的代码,两次调用了析构函数.
Put a breakpoint in your destructor. The main reason to get an exception is that the destrcutor is called twice because of wrong code.


问题不在您的类定义中,而是在使用它的代码中.

尽管...由于您从CObject继承了CSnp,因此应该将析构函数设为虚拟,除非您完全确定没有类会从CSnp继承! (但这很可能不是造成您问题的原因)


可能导致删除期间发生异常
1. CSnp ::〜CSnp()的实现(例如,通过调用虚拟函数或访问已被破坏的其他数据)

2.要求删除时调用delete [],反之亦然

3.重复调用析构函数,e. G.因为在某处直接调用了它(这可能在上面的情况2中发生,或者当您的对象存储在需要特别注意销毁的某个容器中并且您也这样做时)

4.如果不使用调试器,很难确定很多其他原因(换句话说,如果以上都不是,请使用调试器!)
The problem is not in your class definition, it is in the code that uses it.

Although... since you inherit CSnp from CObject you should make your destructor virtual, unless you are absolutely sure that no class will ever inherit from CSnp! (but that is most likely not the cause of your problem)

An exception during delete may be caused by

1. the implementation of CSnp::~CSnp() (e. g. by calling virtual functions or accessing other data that''s already destroyed)

2. calling delete[] when delete was called for or vice versa

3. repeated call of the destructor, e. g. because somewhere it''s called directly (this might happen in case 2 above, or when your object(s) is stored in some container that takes special care of destruction and you do so as well)

4. plenty of other causes that are pretty hard to determine without using the debugger (in other words, if it''s none of the above, use the debugger!)


这篇关于对于使用new创建的类对象,使用delete释放内存.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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