Reputation: 919
The program I am writing can build and get executed from CodeBlocks IDE, but pressing a specific button ( invoking a handler function ) makes program crash. In debugger mode I get this message :
Program received signal SIGSEGV, Segmentation fault.
And it happens in the line :
m_listboxSorted->SetString(k, "some text");
Minimal version of my program, that I hope includes all the important parts of code, is :
class MainFrame: public wxFrame
{
public:
wxListBox *m_listboxSorted;
};
bool TestApp::OnInit()
{
MainFrame* OurMainFrame = new MainFrame(constructor info... );
OurMainFrame->Show( true );
return true;
}
MainFrame::MainFrame(constructor info...)
{
wxListBox * m_listboxSorted = new wxListBox(constructor info...);
}
void MainFrame::ButtonClicked(wxCommandEvent & event)
{
static unsigned int k = 0 ;
k++;
m_listboxSorted->SetString(k, "some text"); // !! Problematic line !!
}
Now, after searching online for solution to the signal SIGSEGV, Segmentation fault I figured that the problem must be in the pointer, and dereferencing it. But as a beginner, I was unsuccessful in applying this information to solve my program issue.
EDIT :
I have also tried to change this line :
wxListBox *m_listboxSorted;
with this one:
wxListBox *m_listboxSorted = new wxListBox;
But this time, when pressing the button ( invoking the handler function ), I get this message :
What to correct in my code to make the program function properly ?
Upvotes: 0
Views: 137
Reputation: 238311
The member wxListBox *m_listboxSorted;
is uninitialized.
wxListBox * m_listboxSorted = new wxListBox(constructor info...);
in the constructor does not assign a value to the member, but instead declares a local pointer which is leaked when the constructor returns.
m_listboxSorted->SetString(k, "some text");
then dereferences the uninitialized member pointer resulting in undefined behaviour.
To fix, remove the type from the line in constructor so that it becomes an assignment rather than a declaration.
Upvotes: 2