【问题标题】:GDI+ leaks memory when deleting pointers as GdiplusBase*?删除指针为 GdiplusBase* 时 GDI+ 会泄漏内存?
【发布时间】:2009-08-11 23:58:10
【问题描述】:

我正在尝试使用 GDI+,但遇到了奇怪的内存泄漏。我有一个vectorGdiplusBase 指针,它们都是动态创建的。不过,奇怪的是,如果我尝试将对象删除为 GdiplusBase 指针,例如,

vector<GdiplusBase*> gdiplus;
gdiplus.push_back(new Image(L"filename.jpg"));
delete gdiplus[0];

对象未删除且内存泄漏(根据任务管理器)。但是,如果我转换回原来的指针然后删除,

delete (Image*)gdiplus[0];

对象已正​​确删除。据我所知,这件事的奇怪之处在于(根据 MSDN)GdiplusBase is the base class of all GDI+ objects and owns the delete operators for all of them。在那种情况下,delete gdiplus[0]; 不应该正常工作并释放内存吗?我在这里做错了吗?

【问题讨论】:

    标签: c++ memory memory-management memory-leaks gdi+


    【解决方案1】:

    我想问题是GdiplusBase 没有虚拟析构函数,因此当您像这样调用delete 时,不会调用析构函数。 Image 的析构函数可能会释放一些其他资源(例如位图句柄等)。因此,Image object 本身的内存被正确释放,但它正在使用的其他资源(也可能耗尽内存)没有被释放。

    【讨论】:

      猜你喜欢
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 2013-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多