c++ - 在用户定义的异常析构函数中释放动态内存

标签 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导致此问题的 block 。但我不知道在这种情况下如何释放内存。你能帮我吗?

代码如下:

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

最佳答案

这个问题可以通过遵循“三法则”来解决。什么是三法则?

来自another SO answer :

If you need to explicitly declare either the destructor, copy constructor or copy assignment operator yourself, you probably need to explicitly declare all three of them.

更多详情请访问another SO questionWikipedia .

#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

关于c++ - 在用户定义的异常析构函数中释放动态内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28471253/

相关文章:

c++ - 为什么 vector 被视为按值传递,即使它是通过 const 引用传递的?

c++ - 在函数中使用 bool 值 "recursiveCall"参数是一种好习惯吗?

c++ - 不能像 store 一样在 x86 上放宽原子 fetch_add 重新排序,稍后加载?

Python:尝试/排除整个模块

c++ - 无法捕获 C++ 异常

C++:用多个定界符拆分字符串并在结果中保留定界符?

c++ - 在 xcode 4 中找不到文件

c# - 进程因 StackOverFlowException C# 而终止

java - java中方法和构造函数中异常的继承

java - 无法在 Spring Autowiring 字段。为什么?