> ## Documentation Index
> Fetch the complete documentation index at: https://docs.abbyy.com/llms.txt
> Use this file to discover all available pages before exploring further.

# DeinitializeEngine Function

This function deinitializes ABBYY FineReader Engine. The function must be called for deinitialization of the Engine object obtained using the [InitializeEngine](/fine-reader/engine/api-reference/functions/initializeengine-function) function. Prior to the deinitialization, you must release all references to FineReader Engine objects.

## Syntax

### C++

```cpp theme={null}
HRESULT __stdcall DeinitializeEngine();
```

### C\#

```csharp theme={null}
int DeinitializeEngine();
```

### Visual Basic .NET

```csharp theme={null}
Private Declare Function DeinitializeEngine Lib "FREngine.dll" () As Integer
```

## Return values

This function returns E\_FAIL if not all objects are released. In that case, you can get the list of not released objects using the [IEngine::StartLogging](/fine-reader/engine/api-reference/engine-object-iengine-interface/supplementary-methods/startlogging-method) method. The function may return the [standard return values of ABBYY FineReader Engine functions](/fine-reader/engine/api-reference/return-codes).

## Remarks

<Warning>
  Do not initialize and deinitialize ABBYY FineReader Engine at the entry points of other dynamic libraries and also in constructors and destructors of static and global objects implemented in dynamic libraries, because they are called at the dynamic library entry points.
</Warning>

A user should initialize and deinitialize ABBYY FineReader Engine elsewhere. For example, in the main or WinMain function of an executable module.

<Note>
  In Windows, this restriction is due to the fact that the Win32 LoadLibrary and FreeLibrary functions are not re-entrant.
</Note>

## Samples

<Accordion title="C++ (COM) code">
  ```cpp theme={null}
  IEngine* FineReaderEngine = 0;
  HMODULE EngineLibraryHandle = 0;
  static HRESULT deinitializeEngine()
  {
   // Release Engine object
   if( FineReaderEngine != 0 ) {
    FineReaderEngine->Release();
    FineReaderEngine = 0;
   }
   // Calling DeinitializeEngine
   typedef HRESULT (STDAPICALLTYPE* DeinitializeEngineFunc)();
   DeinitializeEngineFunc pDeinitializeEngine =
    (DeinitializeEngineFunc)GetProcAddress( EngineLibraryHandle, "DeinitializeEngine" );
   if( pDeinitializeEngine == 0 || FAILED( pDeinitializeEngine() ) ) {
    setLastErrorInfo( L"Can't unload FineReader Engine." );
    return E_UNEXPECTED;
   }
   return S_OK;
  }
  ```
</Accordion>

<Accordion title="C# code">
  ```csharp theme={null}
  public class EngineLoader : IDisposable
  {
      // Unload FineReader Engine
      public void Dispose()
      {
          if (engine == null)
          {
              // Engine was not loaded
              return;
          }
          engine = null;
          // Deleting all objects before FreeLibrary call
          GC.Collect();
          GC.WaitForPendingFinalizers();
          GC.Collect();
          int hresult = deinitializeEngine();
   
          hresult = dllCanUnloadNow();
          if (hresult == 0)
          {
              FreeLibrary(dllHandle);
          }
          dllHandle = IntPtr.Zero;
          initializeEngine = null;
          deinitializeEngine = null;
          dllCanUnloadNow = null;
          // throwing exception after cleaning up
          Marshal.ThrowExceptionForHR(hresult);
      }
      // Kernel32.dll functions
      [DllImport("kernel32.dll")]
      private static extern IntPtr LoadLibraryEx(string dllToLoad, IntPtr reserved, uint flags);
      private const uint LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008;
      [DllImport("kernel32.dll")]
      private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
      [DllImport("kernel32.dll")]
      private static extern bool FreeLibrary(IntPtr hModule);
      // FREngine.dll functions
      [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
      private delegate int InitializeEngine( string customerProjectId, string LicensePath, string LicensePassword, , , , ref FREngine.IEngine engine);
      [UnmanagedFunctionPointer(CallingConvention.StdCall)]
      private delegate int DeinitializeEngine();
      [UnmanagedFunctionPointer(CallingConvention.StdCall)]
      private delegate int DllCanUnloadNow();
      // private variables
      private FREngine.IEngine engine = null;
      // Handle to FREngine.dll
      private IntPtr dllHandle = IntPtr.Zero;
      private InitializeEngine initializeEngine = null;
      private DeinitializeEngine deinitializeEngine = null;
      private DllCanUnloadNow dllCanUnloadNow = null;
  }
  ```
</Accordion>

The function is used in all [code samples](/fine-reader/engine/guided-tour/samples) except the Windows [EnginesPool](/fine-reader/engine/guided-tour/samples#enginespool) sample.

## See also

[InitializeEngine](/fine-reader/engine/api-reference/functions/initializeengine-function)

[Different Ways to Load the Engine Object](/fine-reader/engine/guided-tour/advanced-techniques/programming-aspects/different-ways-to-load-engine)
