View Issue Details

IDProjectCategoryView StatusLast Update
0023247FPCDatabasepublic2017-11-06 15:34
ReporterMichael FuchsAssigned ToMichael Van Canneyt 
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
PlatformWindowsOSOS Version
Product Version2.6.0Product Build 
Target Version2.7.1Fixed in Version2.6.4 
Summary0023247: Memoryleak in SQLite3db.TSqlite
DescriptionAfter creating and destroying of a TSqlite object, some memory is not freed.
Steps To Reproduceprogram SqliteMemoryLeak;
{$mode objfpc}{$H+}

uses
  HeapTrc, {$IFDEF UNIX}{$IFDEF UseCThreads}CThreads,{$ENDIF}{$ENDIF}
  Classes, SysUtils, SQLite3db;

begin
  try
    X := TSqlite.Create('test.sqlite');
  finally
    FreeAndNil(X);
  end;
end.
Additional InformationHeap dump by heaptrc unit
73 memory blocks allocated : 1610/1856
72 memory blocks freed : 1598/1840
1 unfreed memory blocks : 12
True heap size : 229376 (112 used in System startup)
True free heap : 229168
Should be : 229184
Call trace for block $0022D6D0 size 12
  $004015D9 main, line 13 of SqliteMemoryLeak.lpr
  $0040C961
Tagssqlite
Fixed in Revision25458
FPCOldBugId
FPCTarget
Attached Files
  • SqliteMemoryLeak.lpr (283 bytes)
  • sqlite3db.pas.patch (681 bytes)
    Index: packages/sqlite/src/sqlite3db.pas
    ===================================================================
    --- packages/sqlite/src/sqlite3db.pas	(revision 22896)
    +++ packages/sqlite/src/sqlite3db.pas	(working copy)
    @@ -314,6 +314,7 @@
        end;
        if length(InterS) > 0 then Field.add(InterS);
        List_Field.add(Field);
    +   Field.Free;
     end;
     {*************************************************************}
     constructor TSQLite.Create(DBFileName: String);
    @@ -348,6 +349,7 @@
           fError := SQLITE_OK;
        end;
        fMsg := sqlite3_errmsg(fSQLite);
    +   strdispose(name);
     end;
     {*************************************************************}
     destructor TSQLite.Destroy;
    
    sqlite3db.pas.patch (681 bytes)

Relationships

related to 0027260 resolvedMichael Van Canneyt Unable to retrieve data from SQLite3 databases 

Activities

2012-10-30 15:20

 

SqliteMemoryLeak.lpr (283 bytes)

Marco van de Voort

2012-10-31 11:03

manager   ~0063580

It's the stralloced "name" variable. Either must be freed or not be a local variable. (so the destructor can free it)

Michael Fuchs

2012-11-01 08:48

reporter   ~0063589

There are even more memory leaks, if TSqlite.Query is used.

2012-11-01 08:48

 

sqlite3db.pas.patch (681 bytes)
Index: packages/sqlite/src/sqlite3db.pas
===================================================================
--- packages/sqlite/src/sqlite3db.pas	(revision 22896)
+++ packages/sqlite/src/sqlite3db.pas	(working copy)
@@ -314,6 +314,7 @@
    end;
    if length(InterS) > 0 then Field.add(InterS);
    List_Field.add(Field);
+   Field.Free;
 end;
 {*************************************************************}
 constructor TSQLite.Create(DBFileName: String);
@@ -348,6 +349,7 @@
       fError := SQLITE_OK;
    end;
    fMsg := sqlite3_errmsg(fSQLite);
+   strdispose(name);
 end;
 {*************************************************************}
 destructor TSQLite.Destroy;
sqlite3db.pas.patch (681 bytes)

Michael Fuchs

2012-11-01 08:48

reporter   ~0063590

I have created a patch to fix both problems.

LacaK

2012-11-21 13:35

developer   ~0063952

IMO patch is ok, but why not just do in constructor TSQLite.Create:
(without allocationg , copying and disposeing new string)

-var
- name : pchar;
...
- name:=StrAlloc (length(DBFileName)+1);
- strpcopy(name,DBFileName);
   OnData:=@SQLOnData;
- sqlite3_open(name,@fSQLite);
+ sqlite3_open(PAnsiChar(DBFileName),@fSQLite);

Sergei Gorelkin

2012-11-21 14:09

developer   ~0063953

String copy can be necessary if sqlite3_open() modifies the passed string.

LacaK

2012-11-22 06:21

developer   ~0063961

Looking at sources of SQLite Filename parameter is not modified, also there are other places in SQLDB, where is called sqlite3_open in style PChar(FileName).

Michael Van Canneyt

2013-03-11 16:26

administrator   ~0066178

Applied. Thank you.

Jonas Maebe

2014-03-04 16:54

manager   ~0073432

Merged to 2.6.4 in spite of 2.7.1 target version

Issue History

Date Modified Username Field Change
2012-10-30 15:19 Michael Fuchs New Issue
2012-10-30 15:19 Michael Fuchs Status new => assigned
2012-10-30 15:19 Michael Fuchs Assigned To => Joost van der Sluis
2012-10-30 15:20 Michael Fuchs File Added: SqliteMemoryLeak.lpr
2012-10-31 11:03 Marco van de Voort Note Added: 0063580
2012-11-01 08:48 Michael Fuchs Note Added: 0063589
2012-11-01 08:48 Michael Fuchs File Added: sqlite3db.pas.patch
2012-11-01 08:48 Michael Fuchs Note Added: 0063590
2012-11-01 11:52 Michael Fuchs Tag Attached: sqlite
2012-11-21 13:35 LacaK Note Added: 0063952
2012-11-21 14:09 Sergei Gorelkin Note Added: 0063953
2012-11-22 06:21 LacaK Note Added: 0063961
2013-03-11 16:26 Michael Van Canneyt Assigned To Joost van der Sluis => Michael Van Canneyt
2013-03-11 16:26 Michael Van Canneyt Fixed in Revision => 23790
2013-03-11 16:26 Michael Van Canneyt Note Added: 0066178
2013-03-11 16:26 Michael Van Canneyt Status assigned => resolved
2013-03-11 16:26 Michael Van Canneyt Fixed in Version => 2.7.1
2013-03-11 16:26 Michael Van Canneyt Resolution open => fixed
2013-03-11 16:26 Michael Van Canneyt Target Version => 2.7.1
2014-03-04 16:54 Jonas Maebe Fixed in Revision 23790 => 25458
2014-03-04 16:54 Jonas Maebe Note Added: 0073432
2014-03-04 16:54 Jonas Maebe Fixed in Version 2.7.1 => 2.6.4
2015-01-06 11:19 Marco van de Voort Relationship added related to 0027260
2017-11-06 15:34 Michael Fuchs Status resolved => closed