#1 2012-01-08 16:14:39

gaddlord
Member
Registered: 2012-01-02
Posts: 8
Website

Any idea why memory leak is reported

I have the following code

var
  C: TSQLDatabase;

initialization

  sqlite3_initialize; // so sqlite3.c is compiled with SQLITE_OMIT_AUTOINIT defined

  C := TSQLDatabase.Create('C:\database.db3');
  C.DBOpen;

finalization

  C.DBClose;
  FreeAndNil(C);

  sqlite3_shutdown;

at the end of SynSQLite3.pas

It generates:

--------------------------------2012/1/8 18:17:15--------------------------------
This application has leaked memory. The small block leaks are (excluding expected leaks registered by pointer):

21 - 36 bytes: Unknown x 6
37 - 52 bytes: Unknown x 4
53 - 68 bytes: Unknown x 3
69 - 84 bytes: Unknown x 3
85 - 100 bytes: Unknown x 3
485 - 532 bytes: Unknown x 2
1013 - 1124 bytes: Unknown x 1

for

This block was allocated by thread 0xE90, and the stack trace (return addresses) at the time was:
804AEA [System][@GetMem][34]
D0D61C [SynSQLite3.pas][SynSQLite3][@Synsqlite3_malloc][1934]
D108FF [SynSQLite3]
D11013 [SynSQLite3][@Synsqlite3_sqlite3_memory_highwater]
D1147B [SynSQLite3][@Synsqlite3_sqlite3_realloc]
D1143E [SynSQLite3][@Synsqlite3_sqlite3_realloc]
D3BB54 [SynSQLite3][@Synsqlite3_sqlite3_set_authorizer]
D55FC4 [SynSQLite3][@Synsqlite3_sqlite3_errmsg]
D560B8 [SynSQLite3][@Synsqlite3_sqlite3_open]
D0E17E [SynSQLite3.pas][SynSQLite3][TSQLDatabase.DBOpen][2677]
D0DC47 [SynSQLite3.pas][SynSQLite3][TSQLDatabase.$bctr][2394]

Any idea why this happens?

Last edited by gaddlord (2012-01-08 16:15:01)

Offline

#2 2012-01-08 16:30:57

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: Any idea why memory leak is reported

No need to call sqlite3_initialize.

It is already done in the initialization section of the SynSQLite3 unit.

Offline

#3 2012-01-08 18:04:04

gaddlord
Member
Registered: 2012-01-02
Posts: 8
Website

Re: Any idea why memory leak is reported

The code snippet was added exactly in the SynSQLite3.pas unit.

I added this there to show case how to achieve this.

A chopped down to the bare minimum version which demostrates the issue is available at http://dl.dropbox.com/u/3607590/TestSynSQLiteLeak.zip

Seems that 23 malloc Pointers are not having their Free functions called.

Can this be related to a known "pseudo leak" from the past as described here http://old.nabble.com/memory-leak-on-sq … 29415.html?

Last edited by gaddlord (2012-01-08 18:34:12)

Offline

#4 2012-01-09 06:40:13

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: Any idea why memory leak is reported

You initialize sqlite3_initialize twice!
The one present in SynSQLite3 is enough.

You download link just does not work.
I compiled the following program:

procedure Test;
var
  C: TSQLDatabase;
begin
  C := TSQLDatabase.Create('D:\Dev\lib\SQLite3\exe\test.db3');
  C.Free;
end;

And it works OK.

Issue in your code is that TSQLDatabase.Create already makes a C.DBOpen.
It is clearly stated by its documentation: "open an existing database file or create a new one if no file exists".

The DBOpen/DBClose functions are to be used only if you want to close then reopen the DB (for backup purposes, e.g.).
A possible working scheme could be:

procedure Test;
var
  C: TSQLDatabase;
begin
  C := TSQLDatabase.Create('D:\Dev\lib\SQLite3\exe\test.db3'); // open the DB
  C.DBClose; // close it
  C.DBOpen; // re-open DB
  C.DBClose; // close it (not necessary: C.Destroy calls DBClose)
  C.Free;
end;

I've added some description to DBOpen/DBClose to make it clear.

Offline

#5 2012-01-13 00:17:21

gaddlord
Member
Registered: 2012-01-02
Posts: 8
Website

Re: Any idea why memory leak is reported

Please disregard. It turned out to be a dummy error in my code which had oveload instead of override in my custom Reader destructor which never got called and finalization of statements never executed.

My bad.

Offline

Board footer

Powered by FluxBB