GDI+ 删除 GdiplusBase* 指针时会泄漏内存吗?

发布于 2024-08-01 19:03:46 字数 617 浏览 0 评论 0原文

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

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

该对象不会被删除并且内存泄漏(根据任务管理器)。 但是,如果我强制转换回原始指针然后删除,则

delete (Image*)gdiplus[0];

该对象将被正确删除。 据我所知,奇怪的是(根据 MSDN) GdiplusBase 是所有 GDI+ 对象的基类,并拥有所有这些对象的删除运算符。 在这种情况下,delete gdiplus[0]; 不应该正常工作并释放内存吗? 我在这里做错了什么吗?

I'm trying to work with GDI+ and I'm running into a weird memory leak. I have a vector of GdiplusBase pointers, all of them dynamically created. The odd thing is, though, is that if I try to delete the objects as GdiplusBase pointers, e.g.,

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

The object is not deleted and memory leaks (according to Task Manager). However, if I cast back to the original pointer and then delete,

delete (Image*)gdiplus[0];

The object is correctly deleted. The strange this about this, as far as I can tell, is that (according to MSDN) GdiplusBase is the base class of all GDI+ objects and owns the delete operators for all of them. In that case, shouldn't delete gdiplus[0]; work correctly and free the memory? Am I doing anything wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

樱&纷飞 2024-08-08 19:03:46

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

I would imagine the problem is that GdiplusBase does not have a virtual destructor, and thus when you call delete like that, no destructor is called. And the destructor of Image is likely releasing some other resources (such as bitmap handles etc). So the memory for the Image object itself is freed correctly, but other resources it is using (which may also use up memory) aren't freed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文