Re: .NET/Link: Leaking references
- To: mathgroup at smc.vnet.net
- Subject: [mg94874] Re: .NET/Link: Leaking references
- From: Asim <maa48 at columbia.edu>
- Date: Mon, 29 Dec 2008 06:39:58 -0500 (EST)
- References: <gj2c7t$fqq$1@smc.vnet.net>
On Dec 26, 5:38 am, Robinaut <robin.gr... at gmail.com> wrote:
> Hi everyone,
> I wrote a function which converts a mathematica 7 image to a .NET
> bitmap object:
>
> Needs["NETLink`"]
>
> toNETImage[img_Image] :=
> NETBlock[
> Module[{rgbBytes, pixFormat, width, height, objBitmap,
> objBitmapData, objRect, objRGBArray, pt},
>
> InstallNET[];
>
> (*convert image to rgb bytes*)
> rgbBytes =
> System`ImageData[ColorConvert[img, "RGB"], "Byte"] // Flatten;
>
> (*create empty bitmap*)
> width = ImageDimensions[img][[1]];
> height = ImageDimensions[img][[2]];
> LoadNETType["System.Drawing.Imaging.PixelFormat"];
> objBitmap =
> NETNew["System.Drawing.Bitmap", width, height,
> PixelFormat`Format24bppRgb];
>
> (*lock bitmap in memory *)
> objRect = NETNew["System.Drawing.Rectangle", 0, 0, width, height=
];
> LoadNETType["System.Drawing.Imaging.ImageLockMode"];
> objBitmapData =
> objBitmap@
> LockBits[objRect, ImageLockMode`ReadWrite,
> PixelFormat`Format24bppRgb];
>
> (*copy rgb bytes to bitmap data*)
> objRGBArray = MakeNETObject[rgbBytes, "System.Byte[]"];
> Array`Reverse[objRGBArray];
> LoadNETType["System.Runtime.InteropServices.Marshal"];
> Marshal`Copy[objRGBArray, 0, objBitmapData@Scan0, width height*3];
> objBitmap@UnlockBits[objBitmapData];
>
> (*rotate bitmap*)
> LoadNETType["System.Drawing.RotateFlipType"];
> objBitmap@RotateFlip[RotateFlipType`RotateNoneFlipX];
> objBitmap@RotateFlip[RotateFlipType`RotateNoneFlipY];
>
> Return[objBitmap]
> ]
> ]
>
> Now to my problem: When the function is called, it returns the
> desired .NET object. But when I call
>
> LoadedNETObjects[]
>
> afterterwards, a list of referenced .NET objects is returned, which
> were created during the evaluation of the function and which should be
> released by NETBlock[] in the function definition. Even worse,
> everytime the function is called, the list of referenced .NET objects
> growth and memory usage increases with every function call.
>
> How can I avoid increasing memory usage?
>
> Thanks in advance.
>
> Regards,
>
> Robin
Hi
Is ReleaseNETObject[obj] relevant?
Asim