[Mono-winforms-list] Matrix memory management in GDI+ bug

Laurent Debacker Laurent Debacker <debackerl@gmail.com>
Sun, 13 Mar 2005 21:24:17 +0100


> If you call Dispose the resource should be freed, as users will expect.
> Also, Microsoft does works this way.

Well, but it depends what you want. At the end both managed and
unmanaged data live in RAM. So if you store a matrix in managed or
unmanaged space, it always use RAM space. When I call Dispose() on a
object, I expect it to work a bit like delete in C++. That is to just
free up ressources used by the object, but most of the time, big
ressources.

Also I don't think the user expect the framework to duplicate the
matrix in both the managed and unmanaged worlds.

What I think is that the matrix should be either kept in managed or
unmanaged world.

Best solution: (or I think so) To keep the Dispose() philosophy ok.
But then the matrix should be in the managed world. When the Matrix
object would be use in a graphics object, we may just convert it to a
native matrix for libgdiplus. It may be kept for some times, or
immedialely free()ed after being used. If it's kept for some times,
the Dispose() may force it to be free()ed. However if you want to
reused the matrix object with a Graphics object again, a new native
matrix could be generated. This would match the Microsoft behavior.

I'm not sure I understand the patch from Jordi, because if I Dispose()
the Matrix, I won't be able to use it again as I can with Microsoft.

On Sat, 12 Mar 2005 13:15:16 -0500, Jonathan Gilbert
<2a5gjx302@sneakemail.com> wrote:
> At 11:18 AM 12/03/2005 +0100, Laurent Debacker wrote:
> >Hi,
> >
> >There's is a bug in the way mono manage the unmanaged matrix in libgdiplus.
> >
> >What I wanted to do is to save the transform matrix used in a Graphics
> >object, save it in a global variable to reuse it later in other
> >Graphics objects. It's just for optimization purpose.
> >
> >However Mono's System.Drawing.Drawing2D.Matrix doesn't behave like
> >Microsoft's one.
> >
> >With Microsoft I can take the transform matrix from a matrix, even
> >Dispose() it, then give it to another Graphics object, and it still
> >works. I know it's tricky to Dispose() there, but I wanted to do
> >futher research.
> 
> If Microsoft's implementation allows the Matrix to be used after it has
> been Dispose()d, this would tend to indicate that they are actually storing
> the matrix in managed memory, and that while a System.Drawing.Graphics has
> an unmanaged matrix involved with the GDI+ operations, a
> System.Drawing.Drawing2D.Matrix simply duplicates the matrix data in
> managed memory. Changing mono to behave this way would be a lot more
> logical than changing Dispose() to not perform its task.
> 
> This is probably something I could do myself :-) Jordi?
> 
> Jonathan Gilbert
> _______________________________________________
> Mono-winforms-list maillist  -  Mono-winforms-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-winforms-list
>