我正在尝试在一个小型 SQLite 演示项目中使用 C++ 中的 Code::Blocks(Linux Mint 18.1)实现我的第一个 wxGrid。构建没有错误。
但是,我在项目中对 wxGrid 的任何引用都会使应用程序崩溃。
简单代码和 gdb 结果如下。爆炸的第一个引用很简单:Grid1->ClearGrid();
#include "wxForm1.h"
#include <wx/msgdlg.h>
#include <wx/wxsqlite3.h>
#include <wx/grid.h>
//(*InternalHeaders(wxForm1)
#include <wx/string.h>
#include <wx/intl.h>
//*)
//(*IdInit(wxForm1)
const long wxForm1::ID_TEXTCTRL1 = wxNewId();
const long wxForm1::ID_BUTTON1 = wxNewId();
const long wxForm1::ID_BUTTON2 = wxNewId();
const long wxForm1::ID_GRID1 = wxNewId();
//*)
BEGIN_EVENT_TABLE(wxForm1,wxFrame)
//(*EventTable(wxForm1)
//*)
END_EVENT_TABLE()
wxForm1::wxForm1(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
//(*Initialize(wxForm1)
Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
SetClientSize(wxSize(620,420));
Move(wxDefaultPosition);
SetBackgroundColour(wxColour(240,19,19));
txtSQL = new wxTextCtrl(this, ID_TEXTCTRL1, _("SELECT * FROM customers;"), wxPoint(40,48), wxSize(304,29), 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
Button1 = new wxButton(this, ID_BUTTON1, _("Using wxButtonClick"), wxPoint(40,104), wxSize(296,29), 0, wxDefaultValidator, _T("ID_BUTTON1"));
Button2 = new wxButton(this, ID_BUTTON2, _("Open DB - Fill Grid"), wxPoint(40,144), wxSize(296,29), 0, wxDefaultValidator, _T("ID_BUTTON2"));
Grid1 = new wxGrid(this, ID_GRID1, wxPoint(40,184), wxSize(544,168), 0, _T("ID_GRID1"));
Grid1->CreateGrid(4,4);
Grid1->EnableEditing(true);
Grid1->EnableGridLines(true);
Grid1->SetDefaultColSize(115, true);
Grid1->SetDefaultCellFont( Grid1->GetFont() );
Grid1->SetDefaultCellTextColour( Grid1->GetForegroundColour() );
Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&wxForm1::OnButton1Click);
Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&wxForm1::OnButton2Click);
//*)
}
wxForm1::~wxForm1()
{
//(*Destroy(wxForm1)
//*)
}
void wxForm1::OnButton1Click(wxCommandEvent& event) //*** ->GetValue() test ***
{
wxString sqlLine = txtSQL->GetValue();
wxMessageBox(sqlLine);
}
void wxForm1::OnButton2Click(wxCommandEvent& event)
{
wxGrid *Grid1;
wxSQLite3Database* db = new wxSQLite3Database();
wxString testDBName = wxGetCwd() + wxT("/database.db");
db->Open(testDBName);
wxSQLite3ResultSet Res = db->ExecuteQuery(wxString::Format(_("%s"),txtSQL->GetValue()));
Grid1->ClearGrid(); // **** Causes Crash ****
int count = 0;
while (Res.NextRow())
{
Grid1->SetCellValue(count,0,wxString::Format(_("%s"),Res.GetAsString(0)) ) ;
Grid1->SetCellValue(count,1,wxString::Format(_("%i"),Res.GetInt(1)) ) ;
Grid1->SetCellValue(count,2,wxString::Format(_("%s"),Res.GetAsString(2)) ) ;
// Grid1->SetCellValue(count,3,wxString::Format(_("%s"),Res.GetAsString(3)) ) ;
count++;
}
}
这是 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".
*** Caught unhandled unknown exception; terminating
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff50a1820 in ?? () from /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
(gdb) bt
#0 0x00007ffff50a1820 in ?? ()
from /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
Backtrace stopped: Cannot access memory at address 0x7fffffffdb88
(gdb)
如有任何建议,请尽量具体。
最佳答案
这是因为您的函数中有一个本地未初始化的 Grid1
,它隐藏了您在构造函数中初始化的类成员。摆脱它并使用类成员。
关于c++ - Grid1->ClearGrid();和其他对 wxGrid crash app 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43645158/