I'm calling Marshal.ReleaseComObject but com objects are still leaking. How to properly release MODI

  • Thread starter Thread starter DR
  • Start date Start date
D

DR

I'm calling Marshal.ReleaseComObject but com objects are still leaking. How
to properly release MODI.Document??

private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 1000000; i++)
{
MODI.Document miDoc = new MODI.Document();
miDoc.Create("a.gif");
miDoc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
MODI.Image tifImg = (MODI.Image)miDoc.Images[0];
string recSTring = tifImg.Layout.Text;
miDoc.Images.Remove(tifImg);
miDoc.Close(false);
System.Runtime.InteropServices.Marshal.ReleaseComObject(tifImg);
System.Runtime.InteropServices.Marshal.ReleaseComObject(miDoc);
}
}
 
1. Does the Create method of the MODI.Document return an object of some
type? If so, you are not capturing it with a variable and therefore this
object is not getting released by the CLR.

2. How do you know that you have a leak?
 
DR,

is it possible that in the line:
MODI.Image tifImg = (MODI.Image)miDoc.Images[0];

the miDoc.Images reference returns somekind COM object, a collection perhaps
(for example MODIImageCollections)?
If so, you should release also that object.. something like this:

MODIImageCollection imgColl=miDoc.Images;
MODI.Image tifImg=imgColl[0];
System.Runtime.InteropServices.Marshal.ReleaseComObject(imgColl);


DR said:
I'm calling Marshal.ReleaseComObject but com objects are still leaking.
How to properly release MODI.Document??

private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 1000000; i++)
{
MODI.Document miDoc = new MODI.Document();
miDoc.Create("a.gif");
miDoc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
MODI.Image tifImg = (MODI.Image)miDoc.Images[0];
string recSTring = tifImg.Layout.Text;
miDoc.Images.Remove(tifImg);
miDoc.Close(false);

System.Runtime.InteropServices.Marshal.ReleaseComObject(tifImg);

System.Runtime.InteropServices.Marshal.ReleaseComObject(miDoc);
}
}
 
What exactly are you expecting to happen when you call releasecomobject? Out
of curiousity, you can examine the returned value from the call to determine
if there are any references left. If there are, that's a sign of a possible
leak. Otherwise, it isn't.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
 
Back
Top