Memory Leak

Discussions about machine vision support in GdPicture.
Post Reply
danielbryk
Posts: 6
Joined: Mon Mar 02, 2009 11:56 am

Memory Leak

Post by danielbryk » Thu Mar 05, 2009 8:20 pm

The OCR is working fine but after 4 hours of use it falls over with the following error:
-1705 MEM_ALLOC's used

Error: Memfree of NULL pointer!

Fatal error: No error trap defined!
Signal_termination_handler called with signal 0

and this kills my application with no chance for me to catch the exception. Any ideas of how to resolve this or at least stop it from killing the entire application.

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Memory Leak

Post by Loïc » Fri Mar 06, 2009 4:02 pm

Hi,

I need some help to help you :D

- Are you using the OCR in GdPicture ActiveX or .NET ?
- Are you sure the error message came form the OCR engine ?
- Could you give more information about the way you apply OCR process ? (kind of image ?, ocr on area ?, are you using the same object ?, an idea of the number of OCR process...)

Best regards,

Loïc

danielbryk
Posts: 6
Joined: Mon Mar 02, 2009 11:56 am

Re: Memory Leak

Post by danielbryk » Fri Mar 06, 2009 4:14 pm

I am using the ocr in .net
Near 100% sure the error came from the OCR engine as it was in the tesseract.log file created the app does not even throw an error it just dies.
The image is of four numbers and I am using a new object for each OCR attempt. Its anoying as the OCR works every time but it does not release the memory so eventually the app just crashes. As for number it varies from 10 to 30.

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Memory Leak

Post by Loïc » Fri Mar 06, 2009 4:26 pm

Hi,

Could you give me a code snippet of your OCR process ?
Maybe you don't release all image handles ?

Also, if you can send me the image on which the OCR crash it can be a great help for me.

Best regards,

Loïc

danielbryk
Posts: 6
Joined: Mon Mar 02, 2009 11:56 am

Re: Memory Leak

Post by danielbryk » Fri Mar 06, 2009 4:51 pm

My code is as follows and I will email the images now.

Code: Select all

  public String ConvertImageToString(String Filename)
        {
            String rtnValue;
            int iImageID;
            GdPictureImaging oGdPictureImaging = new GdPictureImaging();
            int iImageWidth;
            int iImageHeight;

            // initialise GDPicture
            oGdPictureImaging.SetLicenseNumber("???????????????????????????????????????????????????"); // This is the commercial license key. 
            oGdPictureImaging.SetLicenseNumberOCRTesseract("???????????????????????????????????????????????????"); // This is the commercial license key. 
            // grab the image
            iImageID = oGdPictureImaging.CreateGdPictureImageFromFile(Filename);
            // get the dimensions
            iImageWidth = oGdPictureImaging.GetWidth(iImageID);
            iImageHeight = oGdPictureImaging.GetHeight(iImageID);
            // crop any borders off
            oGdPictureImaging.Crop(iImageID, 1, 1, iImageWidth - 2, iImageHeight - 2);
            // resize the image
            oGdPictureImaging.Resize(iImageID, iImageWidth + 200, iImageHeight + 200, GdPicture.InterpolationMode.InterpolationModeDefault);
            // reduce to 1bpp
            oGdPictureImaging.ConvertTo1Bpp(iImageID);
            oGdPictureImaging.FxBitonalDilate4(iImageID);
            // do the OCR
            oGdPictureImaging.OCRTesseractClear();
            rtnValue = oGdPictureImaging.OCRTesseractDoOCR(iImageID, TesseractDictionary.TesseractDictionaryEnglish, "C:\\Program Files\\GdPicture.NET\\Redist\\OCR\\", "12343576890");
            // release the image
            oGdPictureImaging.OCRTesseractClear();
            oGdPictureImaging.ReleaseGdPictureImage(iImageID);

            return rtnValue;
        }

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Memory Leak

Post by Loïc » Fri Mar 06, 2009 6:49 pm

Ok I probably found your problem.

Try to replace:

Code: Select all

// do the OCR
oGdPictureImaging.OCRTesseractClear();
rtnValue = oGdPictureImaging.OCRTesseractDoOCR(iImageID, TesseractDictionary.TesseractDictionaryEnglish, "C:\\Program Files\\GdPicture.NET\\Redist\\OCR\\", "12343576890");
// release the image
oGdPictureImaging.OCRTesseractClear();
by:

Code: Select all

// do the OCR
rtnValue = oGdPictureImaging.OCRTesseractDoOCR(iImageID, TesseractDictionary.TesseractDictionaryEnglish, "C:\\Program Files\\GdPicture.NET\\Redist\\OCR\\", "12343576890");
// release the image
oGdPictureImaging.OCRTesseractReinit();

Hope this solve your issue.

With best regards,

Loïc

danielbryk
Posts: 6
Joined: Mon Mar 02, 2009 11:56 am

Re: Memory Leak

Post by danielbryk » Fri Mar 06, 2009 7:16 pm

Sorry but this still has not solved the issue. Memory is still not being released and after a number of OCR's it pulls the entire app with no catchable error message.

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Memory Leak

Post by Loïc » Mon Mar 09, 2009 11:44 am

Hi,

Please download the last edition from here: https://www.gdpicture.com/download/downl ... urenet.php


I made tests with success on the images you sent me. See the code below (169000 ocr process without 1 kb of memory leak).

Code: Select all

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim storefile As Directory
        Dim files As String()
        Dim File As String

        files = storefile.GetFiles("C:\Debug\", "*")

        For i As Integer = 1 To 100
            For Each File In files
                Debug.Print(ConvertImageToString(File))
            Next
            Label1.Text = Str(i)
            Application.DoEvents()
        Next i
        MsgBox("end!")
    End Sub


    Public Function ConvertImageToString(ByVal Filename As String) As String

        Dim rtnValue As String
        Dim iImageID As Integer
        Dim oGdPictureImaging As New GdPicture.GdPictureImaging
        Dim iImageWidth As Integer
        Dim iImageHeight As Integer

        ' initialise GDPicture
        oGdPictureImaging.SetLicenseNumber("XXX") ' This is the commercial license key.
        oGdPictureImaging.SetLicenseNumberOCRTesseract("XXX") 'This is the commercial license key.
        ' grab the image
        iImageID = oGdPictureImaging.CreateGdPictureImageFromFile(Filename)
        'get the dimensions
        iImageWidth = oGdPictureImaging.GetWidth(iImageID)
        iImageHeight = oGdPictureImaging.GetHeight(iImageID)
        ' crop any borders off
        oGdPictureImaging.Crop(iImageID, 1, 1, iImageWidth - 2, iImageHeight - 2)
        ' resize the image
        oGdPictureImaging.Resize(iImageID, iImageWidth + 200, iImageHeight + 200, GdPicture.InterpolationMode.InterpolationModeDefault)
        ' reduce to 1bpp
        oGdPictureImaging.ConvertTo1Bpp(iImageID)
        oGdPictureImaging.FxBitonalDilate4(iImageID)
        ' do the OCR
        'oGdPictureImaging.OCRTesseractReinit()
        'oGdPictureImaging.OCRTesseractClear()
        rtnValue = oGdPictureImaging.OCRTesseractDoOCR(iImageID, GdPicture.TesseractDictionary.TesseractDictionaryEnglish, "C:\Program Files\GdPicture.NET\Redist\\OCR\", "12343576890")
        Label2.Text = rtnValue
        'release the image
        'oGdPictureImaging.OCRTesseractClear()
        oGdPictureImaging.ReleaseGdPictureImage(iImageID)

        'oGdPictureImaging = Nothing
        Return rtnValue
    End Function

Best regards,

Loïc

danielbryk
Posts: 6
Joined: Mon Mar 02, 2009 11:56 am

Re: Memory Leak

Post by danielbryk » Mon Mar 09, 2009 5:09 pm

Well you have fixed the memory leak but caused the OCR success rate to drop from 90% to 50%. Please let me know how to get this back up.

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Memory Leak

Post by Loïc » Mon Mar 09, 2009 6:42 pm

Hi,

Well you have fixed the memory leak but caused the OCR success rate to drop from 90% to 50%
:shock:
For me on 100 images 75 are OK... => 75% (which can be improved -> see below).


The problem come from your image processing which is not adapted at all to your images.

With this processing I got around 95% of accuracy and better time performances with the image you sent me:

Code: Select all

oGdPictureImaging.Crop(m_ImageID, 1, 1, oGdPictureImaging.GetWidth(m_ImageID) - 2, oGdPictureImaging.GetHeight(m_ImageID) - 2)
oGdPictureImaging.Scale(m_ImageID, 300, InterpolationMode.InterpolationModeHighQualityBicubic)
oGdPictureImaging.ConvertTo1Bpp(m_ImageID)
oGdPictureImaging.FxBitonalErode4(m_ImageID)
The accuracy can also be improved if you keep the same imaging object to make ocr process. instead creating 1 object per OCR process, try to create one object for 100 process.

Best regards,

Loïc

danielbryk
Posts: 6
Joined: Mon Mar 02, 2009 11:56 am

Re: Memory Leak

Post by danielbryk » Tue Mar 10, 2009 3:27 pm

Thanks, this worked a treat. We now have a system that is working at nearly 100% sucess rate.

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Memory Leak

Post by Loïc » Tue Mar 10, 2009 3:31 pm

Good new !

Thank you for your feedback.

With the image you have you can get near than 99% of accuracy with a bit of research...

The idea is to make 2 OCR process with 2 different image processing & comparing the result in order to keep the process having the best relevance. However this take twice processing time but maybe it's not a problem.

Best regards,

Loïc

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests