Index: System.Data.SQLite/UnsafeNativeMethods.cs ================================================================== --- System.Data.SQLite/UnsafeNativeMethods.cs +++ System.Data.SQLite/UnsafeNativeMethods.cs @@ -954,11 +954,15 @@ /// LD_LIBRARY_PATH. /// /// /// The file name of the native library that was successfully loaded. /// - private static void MaybeUpdateLdLibraryPath( + /// + /// Non-zero if the POSIX shared library path was updated; otherwise, + /// zero. + /// + private static bool MaybeUpdateLdLibraryPath( string fileName ) { string directory = Path.GetDirectoryName(fileName); @@ -978,12 +982,15 @@ { path = directory; } Environment.SetEnvironmentVariable(LdLibraryPath, path); + return true; } } + + return false; } ///////////////////////////////////////////////////////////////////////// /// @@ -1002,11 +1009,17 @@ { IntPtr nativeModuleHandle = UnsafeNativeMethodsPosix.dlopen( fileName, UnsafeNativeMethodsPosix.RTLD_DEFAULT); if (nativeModuleHandle != IntPtr.Zero) - MaybeUpdateLdLibraryPath(fileName); + { + if (MaybeUpdateLdLibraryPath(fileName)) + { + UnsafeNativeMethodsPosix.dlclose(nativeModuleHandle); + nativeModuleHandle = IntPtr.Zero; + } + } return nativeModuleHandle; } #endif #endregion @@ -1142,10 +1155,31 @@ EntryPoint = "dlopen", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, SetLastError = true)] internal static extern IntPtr dlopen(string fileName, int mode); + + ///////////////////////////////////////////////////////////////////////// + /// + /// This is the P/Invoke method that wraps the native Unix dlclose + /// function. See the POSIX documentation for full details on what it + /// does. + /// + /// + /// The handle to the loaded native library. + /// + /// + /// Zero upon success -OR- non-zero on failure. + /// +#if NET_STANDARD_20 + [DllImport("libdl", +#else + [DllImport("__Internal", +#endif + EntryPoint = "dlclose", + CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + internal static extern int dlclose(IntPtr module); ///////////////////////////////////////////////////////////////////////// #region Private Constants ///