简体   繁体   English

当我尝试写入wxListBox(C ++,wxWidgets,CodeBlocks)时出现SIGSEGV,分段错误

[英]I get a SIGSEGV, Segmentation fault, when I try to write to wxListBox ( C++, wxWidgets, CodeBlocks )

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. 我正在编写的程序可以从CodeBlocks IDE生成并执行,但是按下特定的按钮(调用处理程序函数)会使程序崩溃。 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. 现在,在在线搜索信号SIGSEGV的解决方案之后,分割错误我发现问题必须出在指针上,然后取消对它的引用。 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 ? 为了使程序正常运行,我的代码中有哪些更正?

The member wxListBox *m_listboxSorted; 成员wxListBox *m_listboxSorted; is uninitialized. 未初始化。

wxListBox * m_listboxSorted = new wxListBox(constructor info...); wxListBox * m_listboxSorted =新的wxListBox(构造函数信息...);

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"); m_listboxSorted-> SetString(k,“一些文本”);

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. 要解决此问题,请从构造函数的行中删除该类型,以使其成为赋值而不是声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM