c++ throw std::string 内存泄漏
最近写代码发现一个抛std::string 的异常,问题如下
#pragma once
#include <iostream>
using namespace std;
struct MyException : public std::exception
{
std::string s;
MyException(std::string ss) : s(ss) {}
~MyException() throw () {} // Updated
const char* what() const throw() { return s.c_str(); }
};
class Foo
{
public:
void Bar(){throw MyException("it's the end of the world!");}
void Caller()
{
try{
this->Bar();// should throw
}
catch(MyException& caught){
std::cout<<"Got "<<caught.what()<<std::endl;
}
}
};
#include "test.h"
int main()
{
Foo f;
f.Caller();
getchar();
return 0;
}
运行后,发现内存泄漏
TestThrow.exe 中的 0x7512a9f2 处最可能的异常: Microsoft C++ 异常: 内存位置 0x00effa18 处的 MyException。
哪位大神能解决一下呢