c++ - 如何在 C++ 异常类析构函数中释放变量

标签 c++ exception destructor free dynamic-memory-allocation

我正在定义一个新的 C++ 类,它的 what 方法返回一个 char* 类型,其值为作为构造函数传递的整数值。

最初我使用 string 类并从 what 返回字符串数据。

然后我尝试使用 char* 键入以下代码:

/* Define the exception here */
class BadLengthException: public exception
{
  public:
    BadLengthException(int strLength)
    {
        strLen = strLength;
        res = (char*)malloc(strLength+1);
        int resultSize = sprintf(res, "%d", strLen);
    }
    ~BadLengthException() throw()
    {
        free(res);
    }
    virtual const char* what() const throw()
    {
      return res;
    }
  private:
    int strLen;
    char* res;
};

但是我在释放 malloc 分配的变量时遇到了问题:它给出了这个异常:

pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

那是为什么呢?我应该在哪里以及如何释放异常类中的动态分配变量?

编辑

这是一个最小的工作完整示例。该程序将要求用户输入。第一个是一个数字,指定以下输入的数量。其他输入将是字符串。如果字符串短于 5,将引发上述异常。

只需输入:1 然后输入 Me 例如

#include <iostream>
#include <string>
#include <sstream>
#include <exception>
using namespace std;

/* Define the exception here */
class BadLengthException: public exception
{
  public:
    BadLengthException(int strLength)
    {
        strLen = strLength;
        res = (char*)malloc(strLength+1);
        int resultSize = sprintf(res, "%d", strLen);
    }
    ~BadLengthException() throw()
    {
        free(res);
    }
    virtual const char* what() const throw()
    {
      return res;
    }
  private:
    int strLen;
    char* res;
};



bool checkUsername(string username) {
    bool isValid = true;
    int n = username.length();
    if(n < 5) {
        throw BadLengthException(n);
    }
    for(int i = 0; i < n-1; i++) {
        if(username[i] == 'w' && username[i+1] == 'w') {
            isValid = false;
        }
    }
    return isValid;
}

int main() {
    int T; cin >> T;
    while(T--) {
        string username;
        cin >> username;
        try {
            bool isValid = checkUsername(username);
            if(isValid) {
                cout << "Valid" << '\n';
            } else {
                cout << "Invalid" << '\n';
            }
        } catch (BadLengthException e) {
            cout << "Too short: " << e.what() << '\n';
        }
    }
    return 0;
}

编辑 2

使用字符串的原始类如下:这个有效

class BadLengthException: public exception
{
  public:
    BadLengthException(int strLength)
    {
        res = to_string(strLength);
    }
    virtual const char* what() const throw()
    {
      return res.c_str();
    }
  private:
    string res;
};

最佳答案

这与异常无关。复制您的类(class)不安全。

如果您要编写这样的类,则需要使其遵循 rule of three .

发生的事情是你的异常对象被复制,它复制了指针,所以你释放了同一个指针两次。

但最简单的方法是使用 std::string 而不是分配您自己的内存。

class BadLengthException: public exception
{
public:
    BadLengthException(int strLength) : strLen(strLength), res(std::to_string(strLength))
    {
    }
    virtual const char* what() const throw()
    {
      return res.c_str();
    }
  private:
    int strLen;
    std::string res;
};

关于c++ - 如何在 C++ 异常类析构函数中释放变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54536896/

相关文章:

c++ - 如何检查 std::vector<std::string> 的元素是否以某个子字符串开头?

c++ - 使用八叉树算法进行网格渲染

python - 空列表的安全 max() 函数

c++ - 删除放置或非放置新

c# - 我们可以以编程方式比较具有相同分辨率的不同图像吗?

c++ - memcmp 与多重相等比较

java - 删除运行时生成的 XSL 输出文件

java - 抛出异常后调用 main 进行另一次尝试

java - 如何清理 Java 中打开的进程?

C++ 为什么在栈中构造完对象后立即调用析构函数?