我有一个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兆内存中非常稳定。
答案 0 :(得分:1)
原来问题是Dispose。
需要Dispose
对象。更好的是,不要处理对象并重新使用它。只要配置没有改变,您就可以重用该对象。遗憾的是,prewarp在配置中,因此您可能无法重用该对象。在对象离开范围之前调用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
}