MODI泄漏记忆

时间:2010-04-22 18:30:47

标签: c# memory-leaks ocr modi

我有一个应用程序,我正在使用MODI 2007来OCR几个多页面的tiff文件。我发现当我在一个包含几个好的tiff但也有一些无法在Windows图片和传真查看器中打开的tiff的目录上启动它时,MODI也无法对那些“坏”的tiff进行OCR。发生这种情况时,应用程序无法回收MODI用于OCR这些tiff的任何内存。在该工具尝试OCR太多这些“坏”tiff之后,机器内存不足并且应用程序崩溃。我已经尝试了几个来自网络的代码修复,据说修复了任何MODI内存泄漏,但到目前为止没有一个对我有用。我正在粘贴以下代码中的OCRing部分:

                StringBuilder strRecText = new StringBuilder(10000);

                MODI.Document doc1 = new MODI.Document();

                doc1.Create(name);
                try
                {
                    doc1.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);  // this will ocr all pages of a multi-page tiff file
                }
                catch (Exception e)
                {
                    doc1.Close(false); // clean up
                    if (doc1 != null)
                    {
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                        GC.Collect();
                        GC.WaitForPendingFinalizers();


                        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc1);
                        doc1 = null;
                    }

                }

                MODI.Images images = doc1.Images;
                for (int imageCounter = 0; imageCounter < images.Count; imageCounter++)
                {
                    if (imageCounter > 0)
                    {
                        if (!noPageBreakFlag)
                        {
                            strRecText.Append((char)pageBreakChar);
                        }
                    }

                    MODI.Image image = (MODI.Image)images[imageCounter];
                    MODI.Layout layout = image.Layout;
                    strRecText.Append(layout.Text);

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    if (layout != null)
                    {
                        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(layout);
                        layout = null;
                    }
                    if (image != null)
                    {
                        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(image);
                        image = null;
                    }

                }

                File.AppendAllText(ocrFile, strRecText.ToString());     // write the OCR file out to disk

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();

                if (images != null)
                {
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(images);
                    images = null;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();

                doc1.Close(false); // clean up
                if (doc1 != null)
                {
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc1);
                    doc1 = null;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();

1 个答案:

答案 0 :(得分:1)

过去几个月我一直在使用MODI开展一个项目。到目前为止,MODI已经是我尝试过的最准确的OCR引擎,但是它有一些主要的问题在释放资源和崩溃。

我最终构建了一个命令行应用程序,它将图像的路径作为命令行参数,然后将生成的文本保存到文件中并退出。然后,我使用任何需要modi功能的软件来使用此命令行应用程序。这听起来像是一个奇怪的解决方案,但它是解决MODI所带来的内存泄漏问题的一种非常简单直接的方法,因为当命令行进程存在时,它的内存被操作系统释放,因此您不必担心应用程序崩溃或资源没有清理干净。我发现启动命令行exe然后读取它创建的文件所花费的时间与实际OCR图像所花费的时间相比是非常微不足道的,所以你实际上并没有在性能方面损失太多。

相关问题