[Mono-list] issue with pinvoke & memory leak

Chris Turchin chris@turchin.net
Wed, 2 Jun 2004 04:30:01 -0700 (PDT)


hi,

i have the following sitution (not directly mono related but maybe interesting
regardless since a lot of pinvoke is going on here) that I do not know how to
handle cleanly:

i have been playing with  a wrapper for an image library with the following
method:

private static extern FIBITMAP FreeImage_Rescale(FIBITMAP dib, int dst_width,
					int dst_height, FilterOption filter);

I had implemented it like this:

public FreeImage Resample(int newwidth, int newheight,
				FilterOption filter)

which works fine with something like:

FreeImage img2 = img.Resample(100,100,FilterOption.BILINEAR);

but when I do:

img = img.Resample(100,100,FilterOption.BILINEAR);

then the img object is 'replaced' but a I have a big memory leak, since the
Dispose() method which handles the cleanup of unmanaged resources (the img
object has a pointer to the unmanaged image data which is normally released when
the object is disposed) is never called.

Can someone give me a tip how to handle this in a way that ensures that the old
data from img is disposed in the second case above? Actaully, would this same
issue be a problem for all IDisposable objects in managed code (maybe I am not
allowed to do this at all with IDisposable objects) or is this unique to the
managed/unmanaged code in question here?

Thanks for any feedback!

Regards,
--chris