jingwood / d2dlib

A .NET library for hardware-accelerated, high performance, immediate mode rendering via Direct2D.
MIT License
234 stars 40 forks source link

Memory leak in CreateBitmapFromGDIBitmap() #16

Closed nicolasdejong closed 4 years ago

nicolasdejong commented 4 years ago

I'm using this library for a project that opens and closes a D2DForm often. It uses GDI bitmaps to cache because they are expensive to create and I don't want to wait for that when opening the window.

I noticed the equivalent of the code below leads to a memory leak. The below code should, if I understand it correctly, have the same memory use before and after. Unfortunately that seems not to be the case:

Bitmap bitmap = new Bitmap(1000, 1000);
D2DBitmap d2dBitmap = Device.CreateBitmapFromGDIBitmap(bitmap);
d2dBitmap.Dispose();
bitmap.Dispose();

Is this a memory leak or am I doing something wrong and is there another way to reclaim the used memory? Perhaps there is a workaround?

nicolasdejong commented 4 years ago

Turns out, this line is the culprit:

D2D.CreateBitmapFromHBitmap(this.Handle, bmp.GetHbitmap(), useAlphaChannel);

where the GetHbitmap() is not freed.

I have fixed the method and created a pull request for it.

jingwood commented 4 years ago

Thanks very much!

17