SelfTaught
SelfTaught

Reputation: 499

wxWidgets program seg faulting

I just started an irc client project using wxWidgets on OS X (10.11.3 (15D21)).

However, when I compile the code and run it, it seg faults for some reason. I ran the program with gdb and got the output below.

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

My code hasn't strayed to far away from the hello world tutorial on the wxWidgets website so I'm not sure why I'm having this problem. Based on the gdb output, the issue seems to be related to std::string::assign(). The only place i can see this getting called down the line from is the construction of wxFrame where the title gets passed in?

main.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;
}

frame.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);
}

Compiled using the following 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

Here's the dispatch queue from the crash report

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

Upvotes: 3

Views: 925

Answers (1)

VZ.
VZ.

Reputation: 22688

You didn't build the library with -std=c++11 but are using it for your own code, this is probably the problem. The recommended approach under OS X is to use -std=c++11 -stdlib=libc++ for compiling both the library and your own code, i.e. remove your old build directory (you did build in a separate directory, didn't you?), then rerun configure with CXXFLAGS="-std=c++11 -stdlib=libc++" and make. Finally, update your own makefile to use -stdlib=libc++ too.

Upvotes: 3

Related Questions