在一个简单的C++测试应用程序中:代码:Linux上的块,我有一个名为txtselect的WxTeXTCRL,它包含:“从用户选择*”;
当我运行以下程序时,崩溃!

void refreshGrid()
{
wxTextCtrl *txtSelect;
 wxString sqlLine = txtSelect->GetValue();
}

gdb结果如下:
(gdb) run
Starting program: /home/dan/Documents/wxW_Projs/wxSQLi_417/bin/Debug/wxSQLi_417
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x0000000000410662 in refreshGrid ()
    at /home/dan/Documents/wxW_Projs/wxSQLi_417/wxSQLi_417Main.cpp:199
199  wxString sqlLine = txtSelect->GetValue();

(gdb) bt
#0  0x0000000000410662 in refreshGrid ()
    at /home/dan/Documents/wxW_Projs/wxSQLi_417/wxSQLi_417Main.cpp:199
#1  0x0000000000410593 in wxSQLi_417Frame::OnButton2Click (this=0x7143c0,
    event=...)
    at /home/dan/Documents/wxW_Projs/wxSQLi_417/wxSQLi_417Main.cpp:183
#2  0x00007ffff6d461fe in wxAppConsoleBase::CallEventHandler(wxEvtHandler*, wxEventFunctor&, wxEvent&) const ()
   from /usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#3  0x00007ffff6ecc6e7 in wxEvtHandler::ProcessEventIfMatchedan(wxEventTableEntryBase const&, wxEvtHandler*, wxEvent&) ()
   from /usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#4  0x00007ffff6eccace in wxEvtHandler::SearchDynamicEventTable(wxEvent&) ()
   from /usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#5  0x00007ffff6eccb5f in wxEvtHandler::TryHereOnly(wxEvent&) ()
   from /usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#6  0x00007ffff6eccc13 in wxEvtHandler::ProcessEventLocally(wxEvent&) ()
   from /usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#7  0x00007ffff6eccc75 in wxEvtHandler::ProcessEvent(wxEvent&) ()
   from /usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#8  0x00007ffff75f3de8 in wxWindowBase::TryAfter(wxEvent&) ()
   from /usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
#9  0x00007ffff6ecc9e7 in wxEvtHandler::SafelyProcessEvent(wxEvent&) ()
   from /usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
---Type <return> to continue, or q <return> to quit---

我在同一台电脑上有另一个应用程序,它有一个简单的密码演示程序,使用相同的简单代码,工作得很好,还有很多其他的程序。
任何建议都非常感谢。

最佳答案

txtSelect指的是什么地方。您应该创建一个指针指向的对象,然后使用它,如下所示:

wxTextCtrl *txtSelect = new wxTextCtrl();
wxString sqlLine = txtSelect->GetValue();

如果分配失败new将引发异常std::bad_alloc

09-09 19:33