Page 1 of 1

wxSqlite3 Memory leak

Posted: Sun Oct 04, 2009 10:37 pm
by Allonii
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?

Posted: Mon Oct 05, 2009 3:57 pm
by illnatured
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.

Posted: Mon Oct 05, 2009 4:10 pm
by Allonii
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.

Posted: Mon Oct 05, 2009 5:05 pm
by Allonii
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;
}