c++ - wxWidgets 程序段错误

标签 c++ macos segmentation-fault wxwidgets

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

但是,当我编译并运行代码时,由于某种原因它会出现段错误。我用 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 网站上的 hello world 教程太远,所以我不确定为什么会遇到这个问题。根据 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

主要.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

框架.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++" 重新运行配置并制作。最后,更新您自己的 makefile 以也使用 -stdlib=libc++

关于c++ - wxWidgets 程序段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36378129/

相关文章:

c++ - 强制 GCC 将 .cpp 文件编译为 C

c++ - std::multimap 获取两个范围

c++ - 当不需要完整计算时从某种方法提前返回

Qt 上的 C++ : Controlling transparency of Labels and Buttons

java - 数字签名不受信任。 Java 将不允许对此应用程序进行任何访问

c - 请帮我找到代码中的错误(段错误)

mysql - 无法在 mac -xampp install 上从终端运行 mysql

macos - CorePlot 1.5.1 中的错误

c - 不同环境下运行C程序时出现段错误

c++ - 访问双指针导致段错误