System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt

Symptoms

Error "System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." appears during the processing

Cause

This message usually occurs if you try to access a non-existing object or attempt to use the Engine from different threads simultaneously, but don't use Inproc our Outproc loader. 

Resolution

If you work with COM "naked" interface:

Engine object is a singleton, so it must be initialized, used and deinitialized within one and the same STA thread strictly (default apartment in .Net is MTA). Otherwise, program behavior can be unpredictable. This fact is mentioned in FineReader Engine Help, FAQ section "Using the Engine object".

To check how the Engine object is used in your application, you can log current thread ID and apartment state after every Engine method call: ID must be the same and Apartment state must be STA during all the process of Engine usage, from initialization to unloading.

Please make sure that this requirement is fulfilled for your application. If it is not, then you will need to modify your application structure, so the Engine is definitely used in one STA thread. You can, for example, implement all Engine methods within one separate thread and set this thread apartment state to STA before starting it from service main function.

If this does not suit you, please change to in-process or out-of-process COM loading methods. Please check FineReader Engine Help, chapter "Guided Tour" → "Advanced Techniques" → "Programming Aspects" → "Different Ways to Load the Engine Object" for full information.

Was this article helpful?

4 out of 4 found this helpful

Have more questions? Submit a request

Comments

1 comment

  • Avatar

    Akshay Kumar

    I was able to address this issue with by performing below step.

    • Go to the references and update the Interop.FREngine in the references folder of the project i.e. calling FR(shown in the below image). DLL could be found in the below path on the VM that's installed(C:\ProgramData\ABBYY\SDK\12\FineReader Engine\Inc.NET  interops\v4.7). Choose the framework version according to the project.

     

    0

Please sign in to leave a comment.