QScopedArrayPointer 保护我的数据,但它仍然泄漏
#include <QScopedArrayPointer>
#include <QDebug>
#include <stdexcept>
class MyData{
public:
MyData() {
qDebug() << "Construct a data";
}
~MyData() {
qDebug() << "Delete a data";
}
private:
float internal_data_;
};
class MyClass{
QScopedArrayPointer<MyData> data_;
public:
MyClass(){
data_.reset(new MyData[10]);
throw std::runtime_error("Shit happens");
}
};
int main(int argc, char *argv[])
{
MyClass a_class;
return 1;
}
运行该程序将输出:
Construct a data
Construct a data
Construct a data
Construct a data
Construct a data
Construct a data
Construct a data
Construct a data
Construct a data
Construct a data
terminate called after throwing an instance of 'std::runtime_error'
what(): Shit happens
The program has unexpectedly finished.
在runtime_error之前,变量data_已完全创建。为什么没有调用 data_ 析构函数?
另外,在这种情况下如何确保内存不会泄漏?
#include <QScopedArrayPointer>
#include <QDebug>
#include <stdexcept>
class MyData{
public:
MyData() {
qDebug() << "Construct a data";
}
~MyData() {
qDebug() << "Delete a data";
}
private:
float internal_data_;
};
class MyClass{
QScopedArrayPointer<MyData> data_;
public:
MyClass(){
data_.reset(new MyData[10]);
throw std::runtime_error("Shit happens");
}
};
int main(int argc, char *argv[])
{
MyClass a_class;
return 1;
}
Running this program will output:
Construct a data
Construct a data
Construct a data
Construct a data
Construct a data
Construct a data
Construct a data
Construct a data
Construct a data
Construct a data
terminate called after throwing an instance of 'std::runtime_error'
what(): Shit happens
The program has unexpectedly finished.
Right before the runtime_error, the variable data_ has been fully created. Why is it that data_ destructor not called?
Also, how do I make sure memory does not leak in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于您的异常未被捕获并由终止处理程序处理。由于没有
catch
来处理异常,编译器无法知道要“展开”多少内容。如果捕获异常,就会发生破坏。当然,如果您愿意,您可以重新抛出它,例如:输出以下内容:
I think the issue is that your exception is uncaught and is being handled by the terminate handler. Since there is no
catch
to handle the exception, there is no way for the compiler to know how much to "unroll". If you catch the exception, then destruction occurs. You can then of course re-throw it if you like, for example:Outputs the following: