我有以下问题:
每次我抛出以下异常时,我都会收到Valgrind内存泄漏警告:
我能够可靠地将其追溯到程序中的throw MyException{pointer}
语句。 -如果我将其注释掉,则警告已消失。我还有其他多个可以完美运行的异常类。它们之间的唯一区别是指针变量的存在。
编辑:我尝试使用std :: string作为参数使用相同的方法:Valgrind错误仍然存在。
valgrind错误:
144 bytes in 1 blocks are possibly lost in loss record 7 of 15
(文件信息指向我已经提到的throw MyException{pointer}
语句。
我的例外:
// Forward declaration
class PointerClass;
class MyException : public std::exception
{
public:
MyException() = delete;
MyException(PointerClass* pointer) : pointer_{pointer}
{};
MyException(const MyException& other) = default;
MyException& operator=(MyException& other) = default;
~MyException() = default;
const char* what() const noexcept
{
return "my exception.";
}
const PointerClass* getPointer() const noexcept
{
return pointer_;
}
private:
PointerClass* pointer_ = nullptr;
};
有人知道为什么会这样以及如何解决吗?
答案 0 :(得分:0)
我设法解决了这个问题:我的收获是:
catch(Except::CustomException& exception)
{
std::cout << "Exception occurred" << std::endl;
exit(0);
}
是造成所有问题的原因。 现在我用
catch(Except::CustomException& exception)
{
std::cout << "Exception occurred" << std::endl;
flag = true;
}
if(flag)
{
exit(0);
}
不会引起泄漏的