I am learning gui programming with WxSmith in CodeBlock IDE. I created a sample project with following steps
with GDB, I'm getting following error. Starting program: /home/dwft78/Documents/Applications/TEST/bin/Debug/TEST [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.
0x00007ffff79e2256 in wxWindowBase::InformFirstDirection(int, int, int) () from /usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
(gdb) bt
#0 0x00007ffff79e2256 in wxWindowBase::InformFirstDirection(int, int, int) () at /usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
#1 0x00007ffff79b3d95 in wxSizerItem::InformFirstDirection(int, int, int) () at /usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
#2 0x00007ffff79b4500 in wxBoxSizer::RecalcSizes() () at /usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
#3 0x00007ffff79b263e in wxSizer::Layout() () at /usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
#4 0x00007ffff79e6956 in wxWindowBase::Layout() () at /usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
#5 0x00005555555658bf in TESTFrame::TESTFrame(wxWindow*, int) (this=0x555555835000, parent=0x0, id=-1) at /home/dwft78/Documents/Applications/TEST/TESTMain.cpp:101
#6 0x000055555556361b in TESTApp::OnInit() (this=0x5555557cc600) at /home/dwft78/Documents/Applications/TEST/TESTApp.cpp:27
#7 0x0000555555563c81 in wxAppConsoleBase::CallOnInit() (this=0x5555557cc600) at /usr/include/wx-3.0/wx/app.h:93
#8 0x00007ffff7205b3a in wxEntry(int&, wchar_t**) () at /usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#9 0x000055555556357c in main(int, char**) (argc=1, argv=0x7fffffffdec8) at /home/dwft78/Documents/Applications/TEST/TESTApp.cpp:18
(gdb)
I didn't add any code line and all the code is generated by the IDE itself. I'm using Code::block version 20.03. following are the generated files for the project.
following is the screenshot of the project GUI components
following are the generated cpp and header files for the project.
#include "TESTMain.h"
#include <wx/msgdlg.h>
//(*InternalHeaders(TESTFrame)
#include <wx/intl.h>
#include <wx/string.h>
//*)
//helper functions
enum wxbuildinfoformat {
short_f, long_f };
wxString wxbuildinfo(wxbuildinfoformat format)
{
wxString wxbuild(wxVERSION_STRING);
if (format == long_f )
{
#if defined(__WXMSW__)
wxbuild << _T("-Windows");
#elif defined(__UNIX__)
wxbuild << _T("-Linux");
#endif
#if wxUSE_UNICODE
wxbuild << _T("-Unicode build");
#else
wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
}
return wxbuild;
}
//(*IdInit(TESTFrame)
const long TESTFrame::ID_BUTTON1 = wxNewId();
const long TESTFrame::ID_BUTTON2 = wxNewId();
const long TESTFrame::ID_PANEL1 = wxNewId();
const long TESTFrame::idMenuQuit = wxNewId();
const long TESTFrame::idMenuAbout = wxNewId();
const long TESTFrame::ID_STATUSBAR1 = wxNewId();
//*)
BEGIN_EVENT_TABLE(TESTFrame,wxFrame)
//(*EventTable(TESTFrame)
//*)
END_EVENT_TABLE()
TESTFrame::TESTFrame(wxWindow* parent,wxWindowID id)
{
//(*Initialize(TESTFrame)
wxBoxSizer* BoxSizer1;
wxBoxSizer* BoxSizer2;
wxMenu* Menu1;
wxMenu* Menu2;
wxMenuBar* MenuBar1;
wxMenuItem* MenuItem1;
wxMenuItem* MenuItem2;
Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
SetClientSize(wxSize(914,512));
BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
Button1 = new wxButton(this, ID_BUTTON1, _("Label"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
BoxSizer1->Add(Button1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
Button2 = new wxButton(this, ID_BUTTON2, _("Label"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
BoxSizer1->Add(Button2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxSize(268,236), wxTAB_TRAVERSAL, _T("ID_PANEL1"));
BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
Panel1->SetSizer(BoxSizer2);
SetSizer(BoxSizer2);
Layout();
BoxSizer1->Add(Panel1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
SetSizer(BoxSizer1);
MenuBar1 = new wxMenuBar();
Menu1 = new wxMenu();
MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
Menu1->Append(MenuItem1);
MenuBar1->Append(Menu1, _("&File"));
Menu2 = new wxMenu();
MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
Menu2->Append(MenuItem2);
MenuBar1->Append(Menu2, _("Help"));
SetMenuBar(MenuBar1);
StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
int __wxStatusBarWidths_1[1] = { -1 };
int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
SetStatusBar(StatusBar1);
SetSizer(BoxSizer1);
Layout();
Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&TESTFrame::OnQuit);
Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&TESTFrame::OnAbout);
//*)
}
TESTFrame::~TESTFrame()
{
//(*Destroy(TESTFrame)
//*)
}
void TESTFrame::OnQuit(wxCommandEvent& event)
{
Close();
}
void TESTFrame::OnAbout(wxCommandEvent& event)
{
wxString msg = wxbuildinfo(long_f);
wxMessageBox(msg, _("Welcome to..."));
}```
2. TESTMain.h
```#ifndef TESTMAIN_H
#define TESTMAIN_H
//(*Headers(TESTFrame)
#include <wx/button.h>
#include <wx/frame.h>
#include <wx/menu.h>
#include <wx/panel.h>
#include <wx/sizer.h>
#include <wx/statusbr.h>
//*)
class TESTFrame: public wxFrame
{
public:
TESTFrame(wxWindow* parent,wxWindowID id = -1);
virtual ~TESTFrame();
private:
//(*Handlers(TESTFrame)
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
//*)
//(*Identifiers(TESTFrame)
static const long ID_BUTTON1;
static const long ID_BUTTON2;
static const long ID_PANEL1;
static const long idMenuQuit;
static const long idMenuAbout;
static const long ID_STATUSBAR1;
//*)
//(*Declarations(TESTFrame)
wxButton* Button1;
wxButton* Button2;
wxPanel* Panel1;
wxStatusBar* StatusBar1;
//*)
DECLARE_EVENT_TABLE()
};
#endif // TESTMAIN_H```
3. TESTApp.h
```#ifndef TESTAPP_H
#define TESTAPP_H
#include <wx/app.h>
class TESTApp : public wxApp
{
public:
virtual bool OnInit();
};
#endif // TESTAPP_H```
4. TESTApp.cpp
```#include "wx_pch.h"
#include "TESTApp.h"
//(*AppHeaders
#include "TESTMain.h"
#include <wx/image.h>
//*)
IMPLEMENT_APP(TESTApp);
bool TESTApp::OnInit()
{
//(*AppInitialize
bool wxsOK = true;
wxInitAllImageHandlers();
if ( wxsOK )
{
TESTFrame* Frame = new TESTFrame(0);
Frame->Show();
SetTopWindow(Frame);
}
//*)
return wxsOK;
}```
5. wx_pch.h
```#ifndef WX_PCH_H_INCLUDED
#define WX_PCH_H_INCLUDED
// basic wxWidgets headers
#include <wx/wxprec.h>
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#ifdef WX_PRECOMP
// put here all your rarely-changing header files
#endif // WX_PRECOMP
#endif // WX_PCH_H_INCLUDED```
6. TESTFram.wxs
```<?xml version="1.0" encoding="utf-8" ?>
<wxsmith>
<object class="wxFrame" name="TESTFrame">
<size>914,512</size>
<object class="wxBoxSizer" variable="BoxSizer1" member="no">
<object class="sizeritem">
<object class="wxButton" name="ID_BUTTON1" variable="Button1" member="yes">
<label>Label</label>
</object>
<flag>wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL</flag>
<border>5</border>
<option>1</option>
</object>
<object class="sizeritem">
<object class="wxButton" name="ID_BUTTON2" variable="Button2" member="yes">
<label>Label</label>
</object>
<flag>wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL</flag>
<border>5</border>
<option>1</option>
</object>
<object class="sizeritem">
<object class="wxPanel" name="ID_PANEL1" variable="Panel1" member="yes">
<size>268,236</size>
<object class="wxBoxSizer" variable="BoxSizer2" member="no" />
</object>
<flag>wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL</flag>
<border>5</border>
<option>1</option>
</object>
</object>
<object class="wxMenuBar" variable="MenuBar1" member="no">
<object class="wxMenu" variable="Menu1" member="no">
<label>&File</label>
<object class="wxMenuItem" name="idMenuQuit" variable="MenuItem1" member="no">
<label>Quit</label>
<accel>Alt-F4</accel>
<help>Quit the application</help>
<handler function="OnQuit" entry="EVT_MENU" />
</object>
</object>
<object class="wxMenu" variable="Menu2" member="no">
<label>Help</label>
<object class="wxMenuItem" name="idMenuAbout" variable="MenuItem2" member="no">
<label>About</label>
<accel>F1</accel>
<help>Show info about this application</help>
<handler function="OnAbout" entry="EVT_MENU" />
</object>
</object>
</object>
<object class="wxStatusBar" name="ID_STATUSBAR1" variable="StatusBar1" member="yes">
<fields>1</fields>
<widths>-1</widths>
<styles>wxSB_NORMAL</styles>
</object>
</object>
</wxsmith>```
Found some procedures to prevent the crash.
after avoiding above cases, I was able to run the program without a crash.