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

# Final Step: Unload ABBYY FlexiCapture 12 SDK

After finishing your work with ABBYY FlexiCapture 12 SDK, unload the [**Engine**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/engine.md) object. Use the [**DeinitializeEngine**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/deinitializeengine.md) exported function.

> [accordion] C++ (COM) code
>
> ```cpp theme={null}
> FCEngine::IEnginePtr Engine;
> // HANDLE to FCEngine.dll
> static HMODULE s_libraryHandle = 0;
> BOOL UnloadFCEngine()
> {
>   if( s_libraryHandle == 0 ) {
>     return TRUE;
>   }
>   // Release Engine object
>   if( Engine != 0 ) {
>     Engine.Release();
>   }
>   // Check if FCEngine.dll can be unloaded
>   typedef HRESULT (STDAPICALLTYPE* DeinitializeEngineFunc)();
>   DeinitializeEngineFunc pDeinitializeEngine =
>     (DeinitializeEngineFunc)GetProcAddress( s_libraryHandle, "DeinitializeEngine" );
>   if( pDeinitializeEngine == 0 || pDeinitializeEngine() != S_OK ) {
>     return FALSE;
>   }
>   // Now we can free library safely
>   FreeLibrary( s_libraryHandle );
>   s_libraryHandle = 0;
>   return TRUE;
> }
> ```

This procedure is used in the ABBYY FlexiCapture 12 SDK samples which can be found in the **Samples** folder (Start > Programs > ABBYY FlexiCapture 12 SDK > Code Samples Folder).

It is prohibited to deinitialize ABBYY FlexiCapture 12 SDK at the entry points of other DLLs, and also in constructors and destructors of static and global objects implemented in DLLs, because they are called at the DLL entry points. This restriction is due to the fact that the Win32 **FreeLibrary** function is not re-entrant. You should deinitialize ABBYY FlexiCapture 12 SDK elsewhere, for example, in **WinMain** function of an EXE module.

> [!NOTE]
>
> The "Engine deinitialization failed" exception can be thrown during the deinitialization of the **Engine** object if not all of the objects which were created and used by the application have been deleted before the **Engine** object deinitialization. If you work with programming languages which do not have garbage collections (for example, C++), you must either use smart pointer classes (see the samples in C++ (COM)) or release objects that were created by creation methods when they are no longer needed. If all the objects have been deleted, the exception may be caused by the garbage collector operation. If the application is developed in Visual Basic .NET, all objects with the Nothing value are not deleted, they are only marked for deletion. The exact moment when the garbage collector deletes the object is not known. Therefore, you should call the following methods before the deinitialization of the **Engine** object so that the garbage collector deletes the object:

```csharp theme={null}
GC.Collect()
GC.WaitForPendingFinalizers()
```

You can use the [**StartLogging**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/engine-startlogging.md) method of the **Engine** object to get the list of objects that have not been deleted.

## Additionally

ABBYY FlexiCapture 12 SDK provides two [other ways to load and unload the Engine object](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/differentwaystoloadengine.md). These ways provide access to completely thread-safe objects.

Supplementary information on unloading ABBYY FlexiCapture 12 SDK in applications written in different programming languages:

* [Delphi](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/delphi.md)
* [Java](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/usinginjava.md)
* [Scripting languages](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/scriptlanguages.md)
* [General recommendations for all languages](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/programmingaspects.md)

## See also

[Step 1: Load FlexiCapture 12 SDK](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/engine-loading.md)
