Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

OCR memory leak with dotNet 2.0

-------------------------------------------------------------------------------------------
Very urgent : Problem with NIOCR ActivX ... memory leak
-------------------------------------------------------------------------------------------


Problem description :

    - The NI OCR activX has a problem of memory leak, when launching from a dotNet application ( framework 2.0 / MVStudio 2005 )

    - The default example provided by NI doesn't have this kind of problem .
    (...\Program Files\National Instruments\Vision\Examples\MSVB.NET\3. Applications\OCR )
    But this example doesn't use the OCR activX in a realistic manner ! Instead of acquiring a new CWIMAQImage before
            each OCR analysis, the application picks images in a pre loaded buffer.

    - Modification of the Example, to generate the problem ....

        Instead of picking an image in the prelaoded image buffer, a new image is acquired ( readed ) before each
        OCR analysis.

        Doing so ... the used memory increases ....

        I think the OCR, keeps a reference of the input image, when calling the readtext2 method, avoiding the associated memory liberation.
    
The example joined must be installed in a folder beside ...\Program Files\National Instruments\Vision\Examples\MSVB.NET\3. Applications\OCR because
it has relative links to images and abc files.


0 Kudos
Message 1 of 13
(5,927 Views)
I have found a way to limit the memory loss by calling the OCR activ X always with the same CWIMAQImage.

Solution with memory loss....

while .....

       acquire (  newAcquiredImage )

       NiOcr.readText2( newAcquiredImage , textRead )  ===>>>> Generate a memory loss every loop

end while



Solution which works ....

dim imageOcr as new cwimaqImage

while .....

       acquire (  newAcquiredImage )

       imaqVision.copy( newAcquiredImage , imageOcr )
      
       NiOcr.readText2( imageOcr , textRead )

end while

Doing so, instead of loosing a heap of memory at each loop, only the memory of the 'imageOcr is lost once !

0 Kudos
Message 2 of 13
(5,910 Views)
Hello eler,

Thank you for contacting NI Support.

I had a look at your code and I have found why you are seeing your memory usage increase so drastically.
Near the top of your code you declare Dim CurImage As New NationalInstruments.CWIMAQControls.CWIMAQImage which is a global variable that reads in the image.

In your "Private Sub GetNextImage(ByRef Image As NationalInstruments.CWIMAQControls.CWIMAQImage)" function
You have:

        Dim filename As String
        filename = Dir(CurDir() & "\*.bmp")
        Image = New NationalInstruments.CWIMAQControls.CWIMAQImage
        ' Read the image and store it in the collection
        CWIMAQVision1.ReadImage(Image, CurDir() & "\" & filename)

        'Attach the image to the viewer
        CWIMAQViewer1.Attach(CurImage)

Basically each iteration you are creating a new CWIMAQImage called "Image" and you do the ReadImage function with it; your viewer is using the CurImage variable which is a global created at the start of your whole application.  You are not releasing the memory allocated to Image each time. 

Instead of creating a new image each time you read one in, you should use the CurImage global variable to store the information.  Remove the "Image" declaration and change the parameter of "CWIMAQVision1.ReadImage" to CurImage and this should get rid of your memory leak.  The above code should read as this:

        Dim filename As String
        filename = Dir(CurDir() & "\*.bmp")
  
     ' Read the image and store it in the collection
        CWIMAQVision1.ReadImage(CurImage, CurDir() & "\" & filename)

        'Attach the image to the viewer
        CWIMAQViewer1.Attach(CurImage)

I hope this helps!

Cheers,
Rishi L
National Instruments
Applications Engineer
0 Kudos
Message 3 of 13
(5,891 Views)
Thank you for your answer.

I think your right, the solution works fine.

But, the memory leak is still here ! The memory loss doesn't climb at every image acquisition
but a certain amount of memory is still lost !

The problem is not visible here, but if you modify the application by creating a new main form
which creates in a loop an OCR form, the problem will appears.

Ex :

MainForm

       loop
         
                dim form as new formOcr()

                form.showdialog

       end loop




0 Kudos
Message 4 of 13
(5,879 Views)
Hi Eler,

What are you using to find your memory leak?

Looking at your code, is that you are calling an infinite loop that creates a new OCR form on every iteration and you don't release the memory.  You need to do this otherwise you will always see your memory being used and not recovered.

Regards,
Rishi L
National Instruments
Applications Engineer
0 Kudos
Message 5 of 13
(5,861 Views)
Hi,

To view memory leak i use a tool named .Net memory profiler 3.0

In my application, i have to create new ocr 'on the fly' because users can define in a
user interface the different OCR analysis they want to launch. So i don't know at the
begining how much different OCR activX i will need ... so the solution ( my solution ) is to
create new activX when needed !
0 Kudos
Message 6 of 13
(5,851 Views)
Hy,

You seem to know much about vision, so i ask you an other question about OCR ...

I have an other problem generated by the Usage of NI OCR.

Error : 31563 : Not a valid OCR session.

This popup appears, i think, after disposing an OCR activ X. ( Or when creating a new OCR )

The problem is,  that i must click on the OK button to continue ... but my application is not interactive !

So my question is, how can i avoid this message box to appear ! ( When i click on the OK button my application seems to work fine )

Here is a link to a previous message posted on the NI forum :  http://forums.ni.com/ni/board/message?board.id=200&message.id=15972&jump=true#M15972

Thank you very much for your interest !

Manu.

Emmanuel LERCH ( ELER )
Software engineer
TestInView
Clemessy ESE
France

0 Kudos
Message 7 of 13
(5,845 Views)
Hi Eler,

I have been looking through your code, and I can't seem to find the OCR call you are referring to, nor where you are releasing the memory space.  My first thoughts on that error is that something is wrong with your OCR session naming conventions.  You mentioned before that you need to create OCR sessions dynamically.  The loop you posted will create new sessions with the same reference name for all of them; and so even if you are 'closing the session,' it will only apply for one instance, and will not handle the closing of all open sessions.  Try to change your code so that each OCR session has a unique name, and ensure that you are closing them out using the appropriate name as reference.

Beyond that, I would recommend removing lines of code one at a time, until you find the OCR call that makes this error appear.

Rishi L
National Instruments
Applications Engineer
0 Kudos
Message 8 of 13
(5,813 Views)
Hi iRish,

The problem "Error : 31563 : Not a valid OCR session." don't occurs in the example code i join, but in a bigger application.
( Car Dash board analysis with prosillica cameras, using ImaqDx acquisition , with machine vision, imaq vision, and OCR treatment )
So i can't send you my code because the OCR part is only a very little piece of the global project.

I will try to re-explain you my problem :

    - When i dispose an OCR ( on a windows form ), sometimes, a popup with the title 'National instrument vision 8.5' appears ,
     withe the folowing message :
"Error : 31563 : Not a valid OCR session."

    - I looked at the imaqVision reference chm an i found the error message in the list of known errors ... but without more explainations.

So my questions are .....

  • When does this problem occurs, in which case, and why ...
  • Why does a popup appears, if i configure the activX to generate exceptions ( ExceptionOnError = true )
  • Is it possible to replace the message popup by an exception or something like this

The main problem is, that a popup appears in a non-interactive program ! When i click on the OK button, my program continue
working fine without errors ...

About your previous answer ....

You say that i should take care of a session name. ... But when you use the Ni OCR ActivX, there is no sessionId or session name to configure.
I tried to modify the NI OCR example in order to generate many OCR windows, and i tried then to dispose them. ( creation and disposing managed using
a timer ) With these modifications, i have no errors, ... the disposing seems to work fine ....

Thank you very much for your help ...




0 Kudos
Message 9 of 13
(5,801 Views)
Hi iRish,

I have found my bug !!!!

The message ""Error : 31563 : Not a valid OCR session." is due to a 'double' dispose of
the OCR activ X.

In facts, i have 2 objects which have links to the same OCR. Sometimes i had to remove these objects .... so all two objects
called the activX dispose method ..... and BUG !!!!!

You should even have a look to your inner NI Vision code, because i think your  message is sent after  the  OCR activX
trapped a double disposing. You should only  catch the 2nd dispose call, and not show a dialog box.
Doing so, if someone will have the same 'double disposing' problem, the system will be 'error tolerant' !

Thank you very much for your help

Manu.

 
0 Kudos
Message 10 of 13
(5,760 Views)