From d9099daa9c6105432cf4bd239717d3b982bb9713 Mon Sep 17 00:00:00 2001 From: ivost Date: Wed, 16 Sep 2009 08:50:42 +0000 Subject: [PATCH] * fixed InitializeSqlite and ReleaseSqlite functions (segfault after a call to ReleaseSqlite with refcount=0) * changed all shortstrings to ansistrings ({$h+}) git-svn-id: trunk@13719 - --- packages/sqlite/src/sqlite3.inc | 41 ++++++++++++++++----------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/packages/sqlite/src/sqlite3.inc b/packages/sqlite/src/sqlite3.inc index a0e04d9855..b86decb9cd 100644 --- a/packages/sqlite/src/sqlite3.inc +++ b/packages/sqlite/src/sqlite3.inc @@ -1,4 +1,4 @@ -{$mode objfpc} +{$mode objfpc}{$h+} {$ifdef BSD} {$linklib c} @@ -5725,7 +5725,7 @@ procedure ReleaseSQLite; var SQLiteLibraryHandle: TLibHandle; - DefaultLibrary: String = Sqlite3Lib; + SQLiteDefaultLibrary: String = Sqlite3Lib; {$ENDIF LOAD_DYNAMICALLY} implementation @@ -5912,46 +5912,45 @@ var function TryInitialiseSqlite(const LibraryName: string): Boolean; begin - Result := false; - if (RefCount=0) then + if InterlockedIncrement(RefCount) = 1 then begin SQLiteLibraryHandle := LoadLibrary(LibraryName); - Result := (SQLiteLibraryHandle <> nilhandle); + Result := (SQLiteLibraryHandle <> NilHandle); if not Result then + begin + RefCount := 0; Exit; - Inc(RefCount); + end; LoadedLibrary := LibraryName; LoadAddresses(SQLiteLibraryHandle); - end else begin - if (LoadedLibrary <> LibraryName) then - raise EInoutError.CreateFmt(SErrAlreadyLoaded,[LoadedLibrary]); - Inc(RefCount); + end else Result := True; - end; end; procedure InitialiseSQLite; begin - InitialiseSQLite(DefaultLibrary); + InitialiseSQLite(SQLiteDefaultLibrary); end; procedure InitialiseSQLite(LibraryName: String); begin + if (LoadedLibrary <> '') and (LoadedLibrary <> LibraryName) then + raise EInoutError.CreateFmt(SErrAlreadyLoaded,[LoadedLibrary]); + if not TryInitialiseSQLIte(LibraryName) then raise EInOutError.CreateFmt(SErrLoadFailed,[LibraryName]); end; procedure ReleaseSQLite; begin - if RefCount > 1 then - Dec(RefCount) - else - if UnloadLibrary(SQLiteLibraryHandle) then - begin - Dec(RefCount); - SQLiteLibraryHandle := NilHandle; - LoadedLibrary := ''; - end; + if InterlockedDecrement(RefCount) <= 0 then + begin + if SQLiteLibraryHandle <> NilHandle then + UnloadLibrary(SQLiteLibraryHandle); + SQLiteLibraryHandle := NilHandle; + LoadedLibrary := ''; + RefCount := 0; + end; end; {$ENDIF}