QScopedArrayPointer 保护我的数据,但它仍然泄漏

发布于 2024-10-31 20:06:33 字数 972 浏览 3 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山有枢 2024-11-07 20:06:33

我认为问题在于您的异常未被捕获并由终止处理程序处理。由于没有 catch 来处理异常,编译器无法知道要“展开”多少内容。如果捕获异常,就会发生破坏。当然,如果您愿意,您可以重新抛出它,例如:

#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[]) {
    try {
        MyClass a_class;
    } catch (const std::runtime_error &) {
        throw;
    }
}

输出以下内容:

$ ./test2 
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 
Delete a data 
Delete a data 
Delete a data 
Delete a data 
Delete a data 
Delete a data 
Delete a data 
Delete a data 
Delete a data 
Delete a data 
terminate called after throwing an instance of 'std::runtime_error'
  what():  Shit happens
Aborted

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:

#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[]) {
    try {
        MyClass a_class;
    } catch (const std::runtime_error &) {
        throw;
    }
}

Outputs the following:

$ ./test2 
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 
Delete a data 
Delete a data 
Delete a data 
Delete a data 
Delete a data 
Delete a data 
Delete a data 
Delete a data 
Delete a data 
Delete a data 
terminate called after throwing an instance of 'std::runtime_error'
  what():  Shit happens
Aborted
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文