我有一个C#程序,经过一段可变的时间后一直崩溃。我已将其跟踪到OpenALPR,现在在测试程序中重复了该问题。

我基本上要求在while循环中从图像中获取印版。经过一堆迭代后,它失败了。迭代后失败:179、221、516、429、295、150

程序输出:

...
  Iter (219)  No Plates Found in image 71197e9d829d4d429e74a71c983380dc_09032015
134103267.jpg
Config file location provided via API
LBP Time: 0.005ms.
Total Time to process image: 1.916ms.
  Iter (220)  No Plates Found in image 71197e9d829d4d429e74a71c983380dc_09032015
134103267.jpg
Config file location provided via API
LBP Time: 0.003ms.
Total Time to process image: 4.071ms.
  Iter (221)  No Plates Found in image 71197e9d829d4d429e74a71c983380dc_09032015
134103267.jpg
Config file location provided via API

失败信息:
Unhandled Exception:
Unhandled Exception: System.AccessViolationException: Attempted to read or write
 protected memory. This is often an indication that other memory is corrupt.
   at openalprnet.AlprNet.Dispose(Boolean )
System.AccessViolationException: Attempted to read or write protected memory. Th
is is often an indication that other memory is corrupt.
   at alpr.Alpr.{ctor}(Alpr* , basic_string<char\,std::char_traits<char>\,std::a
llocator<char> >* , basic_string<char\,std::char_traits<char>\,std::allocator<ch
ar> >* , basic_string<char\,std::char_traits<char>\,std::allocator<char> >* )
   at openalprnet.AlprNet..ctor(String country, String configFile, String runtim
eDir)
   at AlprTest.Program.Main(String[] args) in C:\Users\foo\Desktop\c#LPR\A
lprTest\Program.cs:line 25

一次,我还收到了另一条错误消息的一部分(不确定其是否相关):Unable to load regex: @###@@。尽管上面的错误指向了CTOR,但是在我的普通应用程序中,我还是在识别调用期间失败了。我也已经在openalprnet.AlprNet.Dispose(Boolean)中看到了(不确定这些堆栈跟踪的准确度),它是从alpr.Alpr.{ctor}(...调用的

我的测试程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using openalprnet;

namespace AlprTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string chipPath = "71197e9d829d4d429e74a71c983380dc_09032015134103267.jpg";
            string confPath = Path.GetFullPath(".\\openalpr.conf");
            string runtimeDirPath = Path.GetFullPath(".\\runtime_data");
            int i = 0;
            while (true)
            {
                ++i;
                try
                {
                    // Look at target velocity and pick a conf file to use.
                    //
                    AlprNet alpr = new AlprNet("us", confPath, runtimeDirPath);

                    if (!alpr.isLoaded())
                    {
                        return;
                    }
                    // Optionally apply pattern matching for a particular region
                    alpr.DefaultRegion = "va"; // was md
                    alpr.DetectRegion = true;

                    AlprResultsNet results = alpr.recognize(chipPath);

                    if (results.plates.Count < 1)
                    {
                        Console.WriteLine("  Iter ({1})  No Plates Found in image {0}", chipPath, i);
                    }
                    else
                    {
                        int j = 0;
                        foreach (var result in results.plates)
                        {
                            Console.WriteLine("Plate {0}: {1} result(s)", ++j, result.topNPlates.Count);
                            Console.WriteLine("  Processing Time: {0} msec(s)", result.processing_time_ms);
                            foreach (var plate in result.topNPlates)
                            {
                                Console.WriteLine("  - {0}\t Confidence: {1}\tMatches Template: {2}", plate.characters,
                                                    plate.overall_confidence, plate.matches_template);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception caught in LPR processing.  Ex={0}", ex);
                    return;
                }
            }
        }
    }
}

该程序取决于openalpr发行版和相应的opencv dll。 openalpr-net.dll, liblept170.dll, opencv_core248.dll, opencv_features2d248.dll, opencv_ffmpeg248.dll, opencv_flann248.dll, opencv_highgui248.dll, opencv_imgproc248.dll, opencv_objdetect248.dll, opencv_video248.dll。它还使用了runtime_data目录(我只是从示例应用程序中复制了该目录),该目录似乎包含训练数据等。

因此,显然,我正在使用C#。我正在使用Visual Studio 2010和.NET 4.0

我假设我使用的是OpenALPR错误,并且实际上没有任何问题。这似乎是一个非常基本的功能。除了修复它之外,为什么这会使我的程序崩溃,又如何捕捉并恢复呢?您会注意到我的try-catch完全无法捕获它,并且崩溃了整个应用程序。

编辑:
在运行测试应用程序时,它以2gig的内存开始,但是随着它的增长而增长。在147个循环后,它以7.7 gig崩溃。

编辑编辑:
在每次迭代后添加对Dispose的调用,现在程序可以稳定地驻留在50-75兆的内存中。

最佳答案

原来的问题是处置。

需要对象的Dispose。更好的是,不要丢弃该对象并重新使用它。只要配置不变,您就可以重用该对象。可悲的是,预扭曲位于配置中,因此您可能无法重用该对象。在对象离开作用域之前调用Dispose

            try
            {
                // Look at target velocity and pick a conf file to use.
                //
                AlprNet alpr = new AlprNet("us", confPath, runtimeDirPath);

                if (!alpr.isLoaded())
                {
                    return;
                }
                // Optionally apply pattern matching for a particular region
                alpr.DefaultRegion = "va"; // was md
                alpr.DetectRegion = true;

                AlprResultsNet results = alpr.recognize(chipPath);

                ...

                alpr.Dispose(); // Dispose of the object so it can clean up
            }

10-02 02:17
查看更多