OpenALPR crash - 尝试读取或写入受保护的内存 [英] OpenALPR crash - Attempted to read or write protected memory

查看:459
本文介绍了OpenALPR crash - 尝试读取或写入受保护的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#程序在一段时间之后就崩溃了。我已经跟踪到OpenALPR,现在已经在测试程序中重复了这个问题。



我基本上要求在一个循环中从一个图像中获取平板。它经过一连串的迭代失败。迭代后失败:179,221,516,429,295,150



程序输出:

  ... 
Iter(219)图像中找不到图片71197e9d829d4d429e74a71c983380dc_09032015
134103267.jpg
通过API提供的配置文件位置
LBP时间:0.005ms 。
总计处理时间:1.916ms。
Iter(220)图像中找不到图片71197e9d829d4d429e74a71c983380dc_09032015
134103267.jpg
通过API提供的配置文件位置
LBP时间:0.003ms。
总计处理时间:4.071ms。
Iter(221)图像中找不到图片71197e9d829d4d429e74a71c983380dc_09032015
134103267.jpg
通过API
提供的配置文件位置

失败消息:

 未处理的异常:
未处理的异常:System.AccessViolationException :尝试读取或写入
受保护的内存。这通常表明其他内存已损坏。
at openalprnet.AlprNet.Dispose(Boolean)
System.AccessViolationException:尝试读取或写入受保护的内存。 Th
通常表明其他内存已损坏。
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> *)
在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:行25

有一次,我也收到另一个错误消息的一部分(不知道是否与之相关):无法加载正则表达式:@ ### @@ 。虽然上面的错误是指向CTOR,但在我的普通应用程序中,我已经在识别呼叫期间失败了。我也看到(不确定这些堆栈跟踪是否准确)它在 openalprnet.AlprNet.Dispose(Boolean)中从 alpr调用。我的测试程序:

 使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.IO;
使用openalprnet;

命名空间AlprTest
{
类程序
{
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
{
//看目标速度并选择一个conf文件使用
//
AlprNet alpr = new AlprNet(us,confPath,runtimeDirPath);

if(!alpr.isLoaded())
{
return;
}
//可选地为特定区域应用模式匹配
alpr.DefaultRegion =va; // was md
alpr.DetectRegion = true;

AlprResultsNet results = alpr.recognize(chipPath);

if(results.plates.Count< 1)
{
Console.WriteLine(Iter({1})图像{0}中找不到图片,chipPath , 一世);
}
else
{
int j = 0;
foreach(var result in results.plates)
{
Console.WriteLine(Plate {0}:{1} result(s),++ j,result.topNPlates.Count );
Console.WriteLine(处理时间:{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(LPR处理中出现异常Ex = {0},ex);
返回;
}
}
}
}
}

该程序取决于openalpr分发和相应的opencv dll。 openalpr-net.dll,liblept170.dll,opencv_core248.dll,opencv_features2d248.dll,opencv_ffmpeg248.dll,opencv_flann248.dll,opencv_highgui248.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 对象。更好的是,不要处理对象并重新使用它。只要配置不更改,您可以重用该对象。可悲的是,prewarp是在配置中,所以你可能无法重用该对象。在对象离开范围之前,调用 Dispose

  try 
{
//查看目标速度并选择要使用的conf文件。
//
AlprNet alpr = new AlprNet(us,confPath,runtimeDirPath);

if(!alpr.isLoaded())
{
return;
}
//可选地为特定区域应用模式匹配
alpr.DefaultRegion =va; // was md
alpr.DetectRegion = true;

AlprResultsNet results = alpr.recognize(chipPath);

...

alpr.Dispose(); //处理对象,以便它可以清理
}


I have a C# program that has been crashing after a variable amount of time. I've tracked it down to OpenALPR and have now duplicated the problem in a test program.

I basically ask to get the plates from an image in a while loop. It fails after a bunch of iterations. Failed after Iterations : 179, 221, 516, 429, 295, 150

Program output:

...
  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

Failure message:

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

One time, I also got part of another error message(not sure if its related or not) : Unable to load regex: @###@@. Although the error above is pointing to the CTOR, I have, in my normal application, had it fail during the recognize call. I've also seen (not sure how accurate these stack traces are) it in openalprnet.AlprNet.Dispose(Boolean) which was called from alpr.Alpr.{ctor}(...

My test program :

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;
                }
            }
        }
    }
}

The program depends on the openalpr distribution and corresponding opencv dlls. 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. It also uses a runtime_data directory (which I simply copied from the sample app) which seems to contain training data and such.

So, obviously, I'm using C#. I'm using Visual Studio 2010 and .NET 4.0

I'm assuming that I'm using OpenALPR wrong and that there is, in fact, nothing wrong with it. This would seem to be a pretty basic function. Aside from fixing it... why does this crash my program and how can I catch this and recover? You notice my try-catch totally fails to catch it and it crashes the whole application.

EDIT : While running the test app, it starts with like 2gig of memory, but it just grows and grows and grows. It crashed with 7.7 gig after 147 loops.

EDIT EDIT : Added in call to Dispose after each iteration and now the program sits pretty steady at 50-75 megs of memory.

解决方案

Turns out the problem was the Dispose.

Need to Dispose of the object. Better yet, don't dispose of the object and re-use it. So long as the config doesn't change you can reuse the object. Sadly, the prewarp is in the config so you likely won't be able to reuse the object. Call Dispose before the object leaves scope.

            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
            }

这篇关于OpenALPR crash - 尝试读取或写入受保护的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆