释放用户定义表达式析构函数中的动态内存

Freeing dynamic memory in user defined exeption's destructor

本文关键字:动态 内存 析构函数 用户 定义 表达式 释放      更新时间:2023-10-16

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

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

我怀疑MyException对象两次超出范围,一次在myfunc()中,另一次在main的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;
    }
}

问题是通过遵循三条规则来解决的。什么是"三条规则"?

来自另一个SO答案:

如果您需要自己显式声明析构函数、复制构造函数或复制赋值运算符,则可能需要显式声明这三个运算符。

更多详细信息,请参阅另一个SO问题和维基百科。

#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;
    }
}

输出:

测试异常