wxWidgets程序分段错误

wxWidgets program seg faulting

本文关键字:错误 分段 程序 wxWidgets      更新时间:2023-10-16

我刚刚在OS X(10.11.3(15D21))上使用wxWidgets启动了一个irc客户端项目。

然而,当我编译代码并运行它时,由于某种原因,它会出现seg错误。我用gdb运行了这个程序,得到了下面的输出。

Program received signal SIGSEGV, Segmentation fault.
0x00007fff9793871a in std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::assign(std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&) () from /usr/lib/libstdc++.6.dylib

我的代码并没有偏离wxWidgets网站上的helloworld教程太远,所以我不确定为什么会出现这个问题。根据gdb的输出,这个问题似乎与std::string::assign()有关。我能看到的唯一一个地方是wxFrame的结构,标题在那里传递?

主要.hpp

#ifndef __main_hpp
#define __main_hpp
#include <sockit.hpp>
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif
#include "frame.hpp"
class IrcClientApp : public wxApp {
public:
    virtual bool OnInit();
};
#endif

main.cpp

#include "main.hpp"
wxIMPLEMENT_APP(IrcClientApp);
bool IrcClientApp::OnInit() {
    IrcClientFrame* frame = new IrcClientFrame("IRC client", wxPoint(100, 100), wxSize(600, 400));
    frame->Show(true);
    return true;
}

帧.hpp

#ifndef __frame_hpp
#define __frame_hpp
class IrcClientFrame : public wxFrame {
private:
    void about(wxCommandEvent&);
    void exit(wxCommandEvent&);
    wxDECLARE_EVENT_TABLE();
public:
    IrcClientFrame(const wxString&, const wxPoint&, const wxSize&);
};
#endif

frame.cpp

#include "main.hpp"
wxBEGIN_EVENT_TABLE(IrcClientFrame, wxFrame)
    EVT_MENU(wxID_ABOUT, IrcClientFrame::about)
wxEND_EVENT_TABLE()
IrcClientFrame::IrcClientFrame(const wxString& title, const wxPoint& pos, const wxSize& size) :
    wxFrame(NULL, wxID_ANY, title, pos, size) {
    // About menu item
    wxMenu* menu_about = new wxMenu;
    menu_about->Append(wxID_ABOUT);
    // Menu bar
    wxMenuBar* menu_bar = new wxMenuBar;
    menu_bar->Append(menu_about, "&Help");
    // Set it
    SetMenuBar(menu_bar);
    CreateStatusBar();
    SetStatusText("This is the status text!");
}
void IrcClientFrame::about(wxCommandEvent& e) {
    wxMessageBox("Hello world", "Hello world", wxOK | wxICON_INFORMATION);
}
void IrcClientFrame::exit(wxCommandEvent& e) {
    Close(true);
}

使用以下Makefile 编译

CC=g++
WXCONFIG=$(shell ../../../deps/bin/wx-config --cxxflags --libs)
INCLUDES=-I../../../src
CFLAGS=-g -std=c++11 -Wall -Wno-inconsistent-missing-override $(INCLUDES)
LDFLAGS=-L../../../deps/lib
BIN=../bin/client
CPPS=main.cpp frame.cpp
all: mkdir_bin examples
examples: $(CPPS)
    $(CC) $(CFLAGS) $(WXCONFIG) $(CPPS) -o $(BIN)
mkdir_bin: ; if [ ! -d ../bin ]; then mkdir ../bin; fi
clean:     ; if [   -d ../bin ]; then rm -rf ../bin; fi

这是来自故障报告的调度队列

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000001
Exception Note:        EXC_CORPSE_NOTIFY
VM Regions Near 0x1:
--> 
    __TEXT                 0000000103d8d000-0000000103d93000 [   24K] r-x/rwx SM=COW  /Users/USER/*
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libstdc++.6.dylib               0x00007fff9793871a std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::assign(std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&) + 26
1   libwx_osx_cocoau_core-3.1.dylib 0x00000001041bcfbf wxNonOwnedWindow::Create(wxWindow*, int, wxPoint const&, wxSize const&, long, wxString const&) + 63
2   libwx_osx_cocoau_core-3.1.dylib 0x00000001041c1311 wxTopLevelWindowMac::Create(wxWindow*, int, wxString const&, wxPoint const&, wxSize const&, long, wxString const&) + 49
3   client                          0x0000000103d912ec wxFrame::wxFrame(wxWindow*, int, wxString const&, wxPoint const&, wxSize const&, long, wxString const&) + 140 (frame.h:35)
4   client                          0x0000000103d90d2f IrcClientFrame::IrcClientFrame(wxString const&, wxPoint const&, wxSize const&) + 175 (frame.cpp:8)
5   client                          0x0000000103d9111d IrcClientFrame::IrcClientFrame(wxString const&, wxPoint const&, wxSize const&) + 45 (frame.cpp:23)
6   client                          0x0000000103d8e9b6 IrcClientApp::OnInit() + 134 (main.cpp:6)
7   libwx_osx_cocoau_core-3.1.dylib 0x00000001041b755e wxApp::CallOnInit() + 158
8   libwx_baseu-3.1.dylib           0x00000001048623f9 wxEntry(int&, wchar_t**) + 121
9   client                          0x0000000103d8e8a6 main + 38 (main.cpp:3)
10  libdyld.dylib                   0x00007fff9740a5ad start + 1

您没有使用-std=c++11构建库,而是将其用于自己的代码,这可能是问题所在。OS X下的推荐方法是使用-std=c++11 -stdlib=libc++编译库和您自己的代码,即删除旧的构建目录(您确实在单独的目录中构建,不是吗?),然后使用CXXFLAGS="-std=c++11 -stdlib=libc++"重新运行configure并生成。最后,更新您自己的makefile以使用-stdlib=libc++