几个小时以来,我一直在尝试从wxWidgets学习如何使用wxListBox。我的“应用程序”基于官方教程,但即使我的代码与原始代码没有什么不同,我也无法让它工作。
我想向 wxListBox 添加一个新项目,但我得到Segmentation fault。这是我的代码:
class CWindow : public wxFrame {
wxBoxSizer *hbox, *vbox;
wxListBox *list;
wxButton *btnAdd, *btnRemove;
public:
CWindow(int w = 640, int h = 480) : wxFrame(NULL, -1, wxT("Test"), wxDefaultPosition, wxSize(w, h)) {
list = new wxListBox(this, wxID_ANY, wxDefaultPosition, wxSize(w-100, h));
btnAdd = new wxButton(this, -1, wxT("Add"));
btnAdd->Connect(wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction) &CWindow::OnNew);
btnRemove = new wxButton(this, -1, wxT("Remove"));
vbox = new wxBoxSizer(wxVERTICAL);
vbox->Add(btnAdd, 1, wxALL | wxEXPAND, 0);
vbox->Add(btnRemove, 1, 0, 0);
hbox = new wxBoxSizer(wxHORIZONTAL);
hbox->Add(list, 1, 0, 0);
hbox->Add(vbox, 0, 0, 0);
SetSizer(hbox);
}
void OnNew(wxCommandEvent &event) {
wxString str = wxGetTextFromUser(wxT("New item:"));
if (str.Len() > 0) list->InsertItems(1, &str, 0); //this line is suspected of causing segfault
}
};
任何想法是什么导致我的问题?
PS。教程示例工作得很好,所以库错误是不可能的。
编辑: gdb调试器报告这个:
0x00007ffff7a8e492 in wxListBoxBase::InsertItems(unsigned int, wxString const*, unsigned int) () from /usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-2.8.so.0
EDIT2:更多gdb转储之前:
Breakpoint 1, CWindow::OnNew (this=0x725cd0, event=...) at listbox.cpp:32
32 if (str.Len() > 0) list->InsertItems(1, &str, 0);
(gdb) print list
warning: can't find linker symbol for virtual table for `CWindow' value
$1 = (wxListBox *) 0x0
(gdb) up
#1 0x00007ffff7586d35 in wxEvtHandler::ProcessEventIfMatches(wxEventTableEntryBase const&, wxEvtHandler*, wxEvent&) ()
from /usr/lib/x86_64-linux-gnu/libwx_baseu-2.8.so.0