Search code examples
databasesqlitewxwidgets

Segfault on wxStringCollection Creation in wxSQLite3


I'm writing an application that uses the wxSQLite3 library, which is a wrapper around libsqlite3 for the wxWidgets cross-platform GUI programming framework. For some reason calling wxSQLite3Database::CreateStringCollection() results in a segfault. (See the example code below.)

What I don't understand is that there is no problem when using wxIntegerCollection; the problem only occurs when using wxStringCollection.

The wxSQLite3 library is compiled with named collection support (according to wxSQLite3Database::HasNamedCollectionSupport()). This problem occurs on Windows/VC++8 and Linux/GCC.

//Sample code to illustrate problem
#include <wx/app.h>
#include <wx/arrstr.h>
#include <wx/wxsqlite3.h>

class Database {
    public:
        Database();
        ~Database() {db.Close();}
        void query(const wxArrayString& array);
    private:
        wxSQLite3Database db;
        wxSQLite3Statement strStmt;
        wxSQLite3StringCollection strCollection;
};

Database::Database() {
    db.Open(wxT(":memory:"));
    db.EnableForeignKeySupport(true);

    // Create and populate tables
    db.ExecuteUpdate(wxT("CREATE TABLE T1(id int primary key, val text);"));
    db.ExecuteUpdate(wxT("INSERT INTO T1 VALUES (1, 'one');"));
    db.ExecuteUpdate(wxT("INSERT INTO T1 VALUES (2, 'two');"));
    db.ExecuteUpdate(wxT("INSERT INTO T1 VALUES (3, 'three');"));
    db.ExecuteUpdate(wxT("INSERT INTO T1 VALUES (4, 'four');"));
    db.ExecuteUpdate(wxT("INSERT INTO T1 VALUES (5, 'five');"));
    db.ExecuteUpdate(wxT("INSERT INTO T1 VALUES (6, 'six');"));

    // Create the collection and statement
    strCollection = db.CreateStringCollection(wxT("valX_list")); //segfaults here
    strStmt = db.PrepareStatement(wxT("select * from T1 where val in valX_list;"));
}

void Database::query(const wxArrayString& array) {
    strStmt.Reset();
    strCollection.Bind(array);
    wxSQLite3ResultSet r_set = strStmt.ExecuteQuery();
    while (r_set.NextRow()) {
        wxPrintf(wxT("ID:%i  Val: %s\n"), 
            r_set.GetInt(wxT("id")),
            r_set.GetString(wxT("val")).c_str()
        );
    }
}

void runTest() {
    Database db;
    wxArrayString vals;
    vals.Add(wxT("two"));
    vals.Add(wxT("four"));
    vals.Add(wxT("six"));
    db.query(vals);
}

int main() {
    wxSQLite3Database::InitializeSQLite();
    runTest();
    wxSQLite3Database::ShutdownSQLite();
    return 0;
}

Solution

  • In your question you didn't state which version of wxSQLite3 you are using. Unfortunately method wxSQLite3StringCollection::operator= had a bug, which was fixed in the latest wxSQLite3 version 3.2.0. That bug most probably causes the segfault.

    If you happen to use a wxSQLite3 version prior to 3.2.0, you should upgrade to version 3.2.0.