在wxSQLite3中创建wxStringCollection的Segfault

时间:2015-01-29 17:21:43

标签: database sqlite wxwidgets

我正在编写一个使用wxSQLite3库的应用程序,该库是用于wxWidgets跨平台GUI编程框架的libsqlite3的包装器。出于某种原因,调用wxSQLite3Database::CreateStringCollection()会导致段错误。 (参见下面的示例代码。)

我不明白使用wxIntegerCollection时没有问题;只有在使用wxStringCollection时才会出现此问题。

使用命名集合支持编译wxSQLite3库(根据wxSQLite3Database::HasNamedCollectionSupport())。在Windows / VC ++ 8和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;
}

1 个答案:

答案 0 :(得分:1)

在您的问题中,您没有说明您使用的是哪个版本的wxSQLite3。不幸的是,方法 wxSQLite3StringCollection :: operator = 有一个错误,在最新的wxSQLite3版本3.2.0中修复了该错误。该错误最有可能导致段错误。

如果您碰巧使用3.2.0之前的wxSQLite3版本,则应升级到版本3.2.0。