mono / libgdiplus

C-based implementation of the GDI+ API
http://www.mono-project.com/
MIT License
329 stars 171 forks source link

Don't dispose of GpMetafile if a GpGraphics is using it #671

Open lambdageek opened 3 years ago

lambdageek commented 3 years ago

Allow GpGraphics to take ownership of GpMetafile on dispose

The Windows GDI+ allows the metafile instance to be disposed before the graphics instance.

To support that, keep track in the GpGraphics of whether it is responsible for freeing the GpMetafile.

If the graphics instance is deleted first, own_metafile is FALSE and nothing happens.

If the metafile instance is disposed first, set own_metafile to TRUE and don't dispose of the metafile. It will be disposed when the graphics instance is deleted.


Also on Windows GDI+ creating more than one graphics instance for a metafile instance throws an OutOfMemoryException, so do the same in GdipGetImageGraphicsContext


This is related to https://github.com/dotnet/runtime/issues/37838

lambdageek commented 3 years ago

Looks like on Win32 GDI+, you can dispose of the metadata or the graphics instances in either order. Instead of asserting, I'm working on extending the lifetime of the metadata object until the graphics instance is disposed if the metadata is disposed first.