I'm trying to implement my first wxGrid in a small SQLite demo project in Code::Blocks in C++, (Linux Mint 18.1). Builds without errors.
But, any reference I make to wxGrid in the project crashes the app.
The simple code and the gdb results follow. The first reference that blows up is simply: 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++;
}
}
This is the gdb result:
(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)
Any advice appreciated, try to be specific please.
It's because you have a local uninitialized Grid1
in your function that is hiding the class member that you initialize in your constructor. Get rid of that and use the class member.