wxSqlite3 Memory leak Topic is solved

In this forum you can discuss database related issues which can be wxWidgets related, but also generic in nature.
Post Reply
Allonii
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed Jun 24, 2009 1:28 pm

wxSqlite3 Memory leak

Post by Allonii » Sun Oct 04, 2009 10:37 pm

Hi!

After a month or two with wxSqlite3 I found what I believe is a nasty memory leak (or maybe my code is no good at all).

Code: Select all

void save(wxString& name, wxString& age){
    //Connect & Open the database.
    wxSQLite3Database* db = new wxSQLite3Database();
    db->Open("database.db");
    //Prepere statement.
    wxSQLite3Statement stmt;
    //NULL is where the id is.
    stmt = db->PrepareStatement("INSERT INTO table1 VALUES (NULL, ?, ?);");
    //Bind the statements.
    stmt.Bind(1, name);
    stmt.Bind(2, age);
    //Execute the update.
    stmt.ExecuteUpdate();
    //Reset the statement.
    stmt.ClearBindings();
    stmt.Reset();
    //Close connection.
    db->Close();
    delete db;
}
If you save the name and age once everything is ok. But if you save like 10 times the memory will go up +5 MB this is no good at all.

I have tried to comment/uncomment a lot of the code above to localize the problem. The conclusion is that it has to do with the two bindings. I have tried a pointer/reference version of wxSQLite3Database..and so on

Someone have an idé on whats going on?
Thanks

Allonii

illnatured
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 234
Joined: Mon May 08, 2006 12:31 pm
Location: Krakow, Poland

Post by illnatured » Mon Oct 05, 2009 3:57 pm

Which version of wxSqlite3 are you using? They have recently fixed some memory leak in wxSQLite3Statement, so upgrading to latest version may be a good idea.
VC++ 2005 / Windows XP / wxWidgets 2.8.9

Allonii
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed Jun 24, 2009 1:28 pm

Post by Allonii » Mon Oct 05, 2009 4:10 pm

Im using 1.9.5, So I downloaded 1.9.6 and in the readme file
it says,
"Fixed a potential memory leak in wxSQLite3Statement class" in 1.9.6. I think this is the problem because I get the leak when using wxSQLite3Statement.

I will try the new version and replay.
Thanks

Allonii

Allonii
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed Jun 24, 2009 1:28 pm

Post by Allonii » Mon Oct 05, 2009 5:05 pm

I could not wait and since no one replied I solved this.

(if someone ever has the same problem)

Explanation:
In my code above I used a ref version of the wxSQLite3Statement so actually the destructor should be called after we exit the save method. But for some reason its not and every time you save, a new wxSQLite3Statement is created. That gave me a 0.5 MB leak each time I called that method. So what I did is looked into the wxsqlite3 source code and found that in the destructor, Finalize is called.

Solution:
Just add stmt.Finalize in the code above. You will have to do this with wxSQLite3ResultSet too. Its important else you will get a memory leak.

Code Should be:

Code: Select all

void save(wxString& name, wxString& age){
    //Connect & Open the database.
    wxSQLite3Database* db = new wxSQLite3Database();
    db->Open("database.db");
    //Prepere statement.
    wxSQLite3Statement stmt;
    //NULL is where the id is.
    stmt = db->PrepareStatement("INSERT INTO table1 VALUES (NULL, ?, ?);");
    //Bind the statements.
    stmt.Bind(1, name);
    stmt.Bind(2, age);
    //Execute the update.
    stmt.ExecuteUpdate();
    //Clear bindings and finalize.
    stmt.ClearBindings();
    stmt.Finalize();
    //Close connection.
    db->Close();
    delete db;
}
Thanks

Allonii

Post Reply