GDI+ 删除 GdiplusBase* 指针时会泄漏内存吗?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想问题是 GdiplusBase 没有虚拟析构函数,因此当您像这样调用delete 时,不会调用析构函数。 并且
Image
的析构函数可能会释放一些其他资源(例如位图句柄等)。 因此,Image
对象本身的内存已正确释放,但它正在使用的其他资源(也可能会耗尽内存)并未释放。I would imagine the problem is that
GdiplusBase
does not have a virtual destructor, and thus when you calldelete
like that, no destructor is called. And the destructor ofImage
is likely releasing some other resources (such as bitmap handles etc). So the memory for theImage
object itself is freed correctly, but other resources it is using (which may also use up memory) aren't freed.