views:

177

answers:

1

Hi all, 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?

Thanks in advance

A: 

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.

Pavel Minaev