在用户定义的exeption的析构函数中释放动态内存

时间:2015-02-12 07:01:26

标签: c++ exception

我正在尝试删除用户定义的异常的析构函数中的动态内存,该异常在用户定义的异常的构造函数中分配。但我得到核心转储,说明内存被释放两次:

user1@ubuntu:~/practice$ ./a.out
Test Exception
*** Error in `./a.out': double free or corruption (fasttop): 0x0000000000f0b0b0 ***
Aborted (core dumped)

我怀疑MyException对象超出范围两次,一个在myfunc(),另一个在主catch块中导致此问题。但在这种情况下,我无法弄清楚如何释放内存。你能救我吗?

以下是代码:

#include<iostream>
#include<exception>
#include<cstring>
using namespace std;

class MyException: public exception
{
    char *msg;
    public:
    MyException(){}
    MyException(char *str)
    {
        msg = new char[strlen(str)+1];
        strcpy(msg,str);
    }
    char *what()
    {
        return msg;
    }
    ~MyException() throw()
    {
        delete msg;
    }
};

class A
{
    public:
    void myfunc() throw(MyException)
    {
        throw MyException((char*)"Test Exception");
    }
};

int main()
{
    try
    {
        A ob;
        ob.myfunc();
    }
    catch (MyException e)
    {
        cout<<e.what()<<endl;
    }
}

1 个答案:

答案 0 :(得分:2)

遵循“三条规则”解决问题。什么是三法则?

来自another SO answer

  

如果您需要自己显式声明析构函数,复制构造函数或复制赋值运算符,您可能需要显式声明它们中的所有三个。

更多细节可以在another SO questionWikipedia找到 {{3}}

#include <iostream>
#include <exception>
#include <cstring>

using namespace std;

class MyException: public exception
{
    char *msg;
    public:
    MyException(const char *str)  // Making the constructor take a `const char*`
                                  // so it can be invoked with a string
                                  // literal
    {
        msg = new char[strlen(str)+1];
        strcpy(msg,str);
    }
    MyException(MyException const& copy)
    {
        msg = new char[strlen(copy.msg)+1];
        strcpy(msg,copy.msg);
    }
    char const* what() const // Making the return type 'const char*' and
                             // making the member function a const. That's
                             // necessary to make it an override of
                             // std::exception::what()
    {
        return msg;
    }
    ~MyException() throw()
    {
        delete msg;
    }
    MyException& operator=(MyException const& rhs)
    {
       if ( this != &rhs )
       {
          delete [] msg;
          msg = new char[strlen(rhs.msg)+1];
          strcpy(msg,rhs.msg);
       }
       return *this;
    }
};

class A
{
    public:
    void myfunc() throw(MyException)
    {
        throw MyException((char*)"Test Exception");
    }
};

int main()
{
    try
    {
        A ob;
        ob.myfunc();
    }
    catch (MyException e)
    {
        cout<<e.what()<<endl;
    }
}

输出:

Test Exception