Home : Optical Character Recognition (OCR) : Avoiding Memory Leaks and AccessViolationException with AbbyyEngine by using STAThread
Q10465 - INFO: Avoiding Memory Leaks and AccessViolationException with AbbyyEngine by using STAThread

NOTE: This article discusses a single aspect of the AbbyyEngine: for a more generalized overview please see
Q10432 - INFO: AbbyyEngine - Overview

DotImage provides an ability to perform OCR using the ABBYY engine (via our AbbyyEngine class).  If you use this engine, then you should create, initialize, use and deinitialize it in a thread with a single-threaded apartment model, for additional info see the link:

Abbyy KB: Using the Engine object

ABBYY doesn't guarantee the work of its engine it MTA-threads, for instance, the violation of this requirement can cause a memory leak or an AccessViolation error inside the engine.

To set up the thread apartment model in C#, you need to mark your application entry-point method (usually it's the Main method) with STAThread attribute, OR, if you run an OCR operation in a separate thread, call the SetApartmentState method with ApartmentState.STA parameter before the separate thread run.

Code Snippet to Mark an Entry-point With STAThread Attribute

using System;

namespace Demo
{
    public static class Program 
    {
        [STAThread]
        public static void Main(params string[] args)
        {

        }
    }
}

Code Snippet to Create an STA Thread

using System.Threading;

namespace Demo
{
    public class Utils
    {
        // threadWorkDelegate is a function that doesn't accept parameters, or accept one parameter as 'object'
        //
type and has a 'void' return type
        // This function should create the ABBYY engine, process images and shutdown it.
        public static Thread CreateSTAThread(ThreadStart threadWorkDelegate)
        {
            var thread = new Thread(threadWorkDelegate);
            thread.SetApartmentState(ApartmentState.STA);

            //Other thread setup staff like making it background or not, set priority or culture settings.
            //thread.IsBackground = true;
            //thread.CurrentCulture = new CultureInfo("ru-ru");
            //etc.

            return thread;
        }
   }
}

For Visual Basic, the default apartment model is STA, so no additional actions required.


Additional Resource Materials / Related Articles

Q10432 - INFO: AbbyyEngine - Overview

MS KB: Understanding and Using COM Threading Models

Abbyy KB: Using the Engine object

ABBYY KB: Message “Attempted to read or write protected memory”

OCRSDK.com KB: The filename or extension is too long


 

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.

Powered By InstantKB.NET v1.3
Copyright © 2002, 2019. InstantASP Ltd. All Rights Reserved