Attempted to read or write protected memory

Why does the “System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.” error message appears?

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

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



Please sign in to leave a comment.

Recently viewed