wxStyledTextCtrl - crash when only using constructor Topic is solved

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
sorinev
Knows some wx things
Knows some wx things
Posts: 40
Joined: Wed Jan 06, 2016 5:32 pm

wxStyledTextCtrl - crash when only using constructor

Post by sorinev »

OS : Windows 10 Pro x64
IDE : Code::Blocks
tool chain : TDM-GCC-32 5.1.0
wxWidgets : 3.0.2 (SHARED=0 UNICODE=1 CXXFLAGS+="-std=gnu++11" for both release and debug), built with above tool chain

I have a minimal (according to Code::Blocks project wizard) frame based wxWidgets project. I am trying to follow some of the basic stuff outlined here for using wxStyledTextCtrl. When I build in release, it crashes upon running, but when I build in debug, it runs fine (or it seems to). I commented out everything relating to the wxStyledTextCtrl stuff except for the constructor, but it still crashes. Since it builds without error (and the only warnings are 177 wx deprecation warnings), it's not obvious to me what the error could be.

Release:

Code: Select all

-------------- Clean: Release in LogSearch (compiler: GNU GCC Compiler)---------------

Cleaned "LogSearch - Release"

-------------- Build: Release in LogSearch (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -std=c++11 -pipe -mthreads -Wno-deprecated-declarations -D__GNUWIN32__ -D__WXMSW__ -DwxUSE_UNICODE -O2 -Wno-unused-local-typedefs -IC:\wxWidgets-3.0.2\include -IC:\wxWidgets-3.0.2\lib\gcc_lib\mswu -c C:\Users\me\Documents\Projects\LogSearch\LogSearchApp.cpp -o obj\Release\LogSearchApp.o
mingw32-g++.exe -Wall -std=c++11 -pipe -mthreads -Wno-deprecated-declarations -D__GNUWIN32__ -D__WXMSW__ -DwxUSE_UNICODE -O2 -Wno-unused-local-typedefs -IC:\wxWidgets-3.0.2\include -IC:\wxWidgets-3.0.2\lib\gcc_lib\mswu -c C:\Users\me\Documents\Projects\LogSearch\LogSearchMain.cpp -o obj\Release\LogSearchMain.o
windres.exe -IC:\wxWidgets-3.0.2\include -IC:\wxWidgets-3.0.2\lib\gcc_lib\mswu  -J rc -O coff -i C:\Users\me\DOCUME~1\Projects\LOGSEA~1\resource.rc -o obj\Release\resource.res
mingw32-g++.exe -LC:\wxWidgets-3.0.2\lib\gcc_lib -LC:\wxWidgets-3.0.2\lib\gcc_lib -o bin\Release\LogSearch.exe obj\Release\LogSearchApp.o obj\Release\LogSearchMain.o  obj\Release\resource.res -s -mthreads  -lwxmsw30u_stc -lwxscintilla -lwxmsw30u_aui -lwxmsw30u_core -lwxbase30u -lwxpng -lwxzlib -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -luuid -lcomctl32 -lwsock32 -lodbc32 -mwindows
Output file is bin\Release\LogSearch.exe with size 8.66 MB
Process terminated with status 0 (0 minute(s), 3 second(s))
0 error(s), 0 warning(s) (0 minute(s), 3 second(s))
Debug:

Code: Select all

-------------- Clean: Debug in LogSearch (compiler: GNU GCC Compiler)---------------

Cleaned "LogSearch - Debug"

-------------- Build: Debug in LogSearch (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -std=c++11 -pipe -mthreads -Wno-deprecated-declarations -D__GNUWIN32__ -D__WXMSW__ -DwxUSE_UNICODE -g -Wno-unused-local-typedefs -IC:\wxWidgets-3.0.2\include -IC:\wxWidgets-3.0.2\lib\gcc_lib\mswu -c C:\Users\me\Documents\Projects\LogSearch\LogSearchApp.cpp -o obj\Debug\LogSearchApp.o
mingw32-g++.exe -Wall -std=c++11 -pipe -mthreads -Wno-deprecated-declarations -D__GNUWIN32__ -D__WXMSW__ -DwxUSE_UNICODE -g -Wno-unused-local-typedefs -IC:\wxWidgets-3.0.2\include -IC:\wxWidgets-3.0.2\lib\gcc_lib\mswu -c C:\Users\me\Documents\Projects\LogSearch\LogSearchMain.cpp -o obj\Debug\LogSearchMain.o
windres.exe -IC:\wxWidgets-3.0.2\include -IC:\wxWidgets-3.0.2\lib\gcc_lib\mswu  -J rc -O coff -i C:\Users\me\DOCUME~1\Projects\LOGSEA~1\resource.rc -o obj\Debug\resource.res
mingw32-g++.exe -LC:\wxWidgets-3.0.2\lib\gcc_lib -LC:\wxWidgets-3.0.2\lib\gcc_lib -o bin\Debug\LogSearch.exe obj\Debug\LogSearchApp.o obj\Debug\LogSearchMain.o  obj\Debug\resource.res -mthreads  -lwxmsw30ud_stc -lwxscintillad -lwxmsw30ud_aui -lwxmsw30ud_core -lwxbase30ud -lwxpngd -lwxzlibd -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -luuid -lcomctl32 -lwsock32 -lodbc32 -mwindows
Output file is bin\Debug\LogSearch.exe with size 82.40 MB
Process terminated with status 0 (0 minute(s), 8 second(s))
0 error(s), 0 warning(s) (0 minute(s), 8 second(s))
The compile and build calls appear to be the same except for using wx debug libraries instead of release for the debug build. As mentioned, the debug build will run fine, but the release build crashes immediately upon execution. When I change the release build options to take out -s and -O2 and to include -g (release with debug symbols...) and then run it in debug mode (tdm-gcc-32 gdb32.exe), it shows that there is a segmentation fault, and the only function on the call stack is wxCheckDynamicCast(wxObject*, wxClassInfo*)(). Googling hasn't helped.

For reference, here is the source file; I have added very little to the stuff auto-generated by Code::Blocks.

Code: Select all

#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#include "LogSearchMain.h"

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__WXMAC__)
        wxbuild << _T("-Mac");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

BEGIN_EVENT_TABLE(LogSearchFrame, wxFrame)
    EVT_MENU(idMenuOpen, LogSearchFrame::OnOpen)
    EVT_CLOSE(LogSearchFrame::OnClose)
    EVT_MENU(idMenuQuit, LogSearchFrame::OnQuit)
    EVT_MENU(idMenuAbout, LogSearchFrame::OnAbout)
END_EVENT_TABLE()

LogSearchFrame::LogSearchFrame(wxFrame *frame, const wxString& title)
    : wxFrame(frame, -1, title)
{
#if wxUSE_MENUS
    // create a menu bar
    wxMenuBar* mbar = new wxMenuBar();
    wxMenu* fileMenu = new wxMenu(_T(""));
    fileMenu->Append(idMenuOpen, _("&Open\tCtrl-O"), _("Open"));
    fileMenu->Append(idMenuQuit, _("&Quit\tAlt-F4"), _("Quit"));
    mbar->Append(fileMenu, _("&File"));

    wxMenu* helpMenu = new wxMenu(_T(""));
    helpMenu->Append(idMenuAbout, _("&About\tF1"), _("Show info about this application"));
    mbar->Append(helpMenu, _("&Help"));

    SetMenuBar(mbar);
#endif // wxUSE_MENUS

#if wxUSE_STATUSBAR
    // create a status bar with some information about the used wxWidgets version
    CreateStatusBar(2);
    SetStatusText(_("Hello!"),0);
    SetStatusText(_("Powered by ") + wxbuildinfo(short_f), 1);
#endif // wxUSE_STATUSBAR
	textarea = new wxStyledTextCtrl(this, idTextArea);
	//textarea->SetMarginWidth(0, 25);
	//textarea->SetMarginType(0, wxSTC_MARGIN_NUMBER);
	//textarea->StyleSetForeground(wxSTC_STYLE_LINENUMBER, wxColor(0, 0, 0));
	//textarea->StyleSetBackground(wxSTC_STYLE_LINENUMBER, wxColor(235, 235, 235));
	//textarea->SetWrapMode(wxSTC_WRAP_WORD);
	//textarea->SetText(_("Initial text here"));


	//wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
	//sizer->Add(textarea, 1, wxEXPAND);
	//SetSizer(sizer);
}

LogSearchFrame::~LogSearchFrame()
{
}

void LogSearchFrame::OnOpen(wxCommandEvent &event)
{
	
}

void LogSearchFrame::OnClose(wxCloseEvent &event)
{
    Destroy();
}

void LogSearchFrame::OnQuit(wxCommandEvent &event)
{
    Destroy();
}

void LogSearchFrame::OnAbout(wxCommandEvent &event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

Code: Select all

#ifndef LOGSEARCHMAIN_H
#define LOGSEARCHMAIN_H

#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

#include "LogSearchApp.h"
#include <wx/stc/stc.h>

class LogSearchFrame: public wxFrame
{
    public:
        LogSearchFrame(wxFrame *frame, const wxString& title);
        ~LogSearchFrame();
    private:
        enum
        {
            idMenuOpen = 1000,
            idMenuQuit,
            idMenuAbout,
            idTextArea
        };

        wxStyledTextCtrl* textarea;

        void OnOpen(wxCommandEvent& event);
        void OnClose(wxCloseEvent& event);
        void OnQuit(wxCommandEvent& event);
        void OnAbout(wxCommandEvent& event);
        DECLARE_EVENT_TABLE()
};


#endif // LOGSEARCHMAIN_H
If I comment out the line below, release runs fine.

Code: Select all

textarea = new wxStyledTextCtrl(this, idTextArea);
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxStyledTextCtrl - crash when only using constructor

Post by PB »

Just to make sure and rule out (not very likely) possibility that something is wrong with your wxWidgets build (did you build it yourself or do you use the precompiled one?): Have you tried to test the bundled stc sample compiled using the provided gcc makefile with the same build options as your sample application?

Also I don't see the definition of idTextArea, I would try to using wxID_ANY just in case this ID clashes with another. But this is even less likely than the above, but still....
sorinev
Knows some wx things
Knows some wx things
Posts: 40
Joined: Wed Jan 06, 2016 5:32 pm

Re: wxStyledTextCtrl - crash when only using constructor

Post by sorinev »

PB wrote:Just to make sure and rule out (not very likely) possibility that something is wrong with your wxWidgets build (did you build it yourself or do you use the precompiled one?): Have you tried to test the bundled stc sample compiled using the provided gcc makefile with the same build options as your sample application?

Also I don't see the definition of idTextArea, I would try to using wxID_ANY just in case this ID clashes with another. But this is even less likely than the above, but still....
The code quote at the bottom of the post has the header, which is where idTextArea is declared. The top of the post shows my wxWidgets state - I built it from the source .7z file on the site with the options I mention at the top (static and unicode).

I compiled and ran the stc sample in both debug and release and it ran ok.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxStyledTextCtrl - crash when only using constructor

Post by PB »

I see, I can't see anything wrong with the code. The only issue I see is that you used different C++11 option (-std=gnu++11vs -std=c++11 when building wxWidgets and your app. But I guess these are interchangeable - I'm not really using GCC much?
sorinev
Knows some wx things
Knows some wx things
Posts: 40
Joined: Wed Jan 06, 2016 5:32 pm

Re: wxStyledTextCtrl - crash when only using constructor

Post by sorinev »

PB wrote:I see, I can't see anything wrong with the code. The only issue I see is that you used different C++11 option (-std=gnu++11vs -std=c++11 when building wxWidgets and your app. But I guess these are interchangeable - I'm not really using GCC much?
I did catch the difference in the -std option a little bit ago, but I changed it to match and it didn't help. I still don't know why release would crash and debug would run on this one single line despite the same compile and link options (aside from using debug versions of the libraries). I've tried filtering out the various differences between my project and the sample in terms of the build commands, but I'm still not seeing a difference.
sorinev
Knows some wx things
Knows some wx things
Posts: 40
Joined: Wed Jan 06, 2016 5:32 pm

Re: wxStyledTextCtrl - crash when only using constructor

Post by sorinev »

To shed a little more light on this, since I'm using wxWidgets statically, I can just drop the binary onto different systems (Windows anyway). My system is Windows 10. I've tried it on another Windows 10 system, two Windows 7 systems, and a Windows XP system. It crashes upon execution on both Windows 10 systems and on the Windows XP system, but it starts and runs without incident on the two Windows 7 systems. Maybe the issue is still there and through a fluke it works, but in any case, hopefully this sheds some light on things.
sorinev
Knows some wx things
Knows some wx things
Posts: 40
Joined: Wed Jan 06, 2016 5:32 pm

Re: wxStyledTextCtrl - crash when only using constructor

Post by sorinev »

Actually got more debug information this time.

Code: Select all

#00 00ACE625	IsKindOf(info=0xcb99c8 <wxRadioButton::ms_classInfo>, this=<optimized out>) (../../include/wx/rtti.h:91)
#01 ??			wxCheckDynamicCast (obj=0x94ab810, classInfo=0xcb99c8 <wxRadioButton::ms_classInfo>) (../../include/wx/object.h:450)
#02 005AFF8F	wxSetFocusToChild(win=0x94a9c28, childLastFocused=0x94a9dc8) (../../src/common/containr.cpp:736)
#03 004711A0	DoRestoreLastFocus(this=<optimized out>) (../../src/msw/toplevel.cpp:1447)
#04 ??			wxTopLevelWindowMSW::OnActivate (this=<optimized out>, event=...) (../../src/msw/toplevel.cpp:1469)
#05 00722A72	wxAppConsoleBase::HandleEvent(this=<optimized out>, handler=0x94a9c28, func=<optimized out>, event=...) (../../src/common/appbase.cpp:611)
#06 00722F57	wxAppConsoleBase::CallEventHandler(this=0x949bc70, handler=0x94a9c28, functor=..., event=...) (../../src/common/appbase.cpp:623)
#07 0073E910	wxEvtHandler::ProcessEventIfMatchesId(entry=..., handler=0x94a9c28, event=...) (../../src/common/event.cpp:1392)
#08 0073E9FA	wxEventHashTable::HandleEvent(this=<optimized out>, event=..., self=self@entry=0x94a9c28) (../../src/common/event.cpp:998)
#09 0073EF04	wxEvtHandler::TryHereOnly(this=0x94a9c28, event=...) (../../src/common/event.cpp:1589)
#10 0073EF8A	TryBeforeAndHere(event=..., this=0x94a9c28) (../../include/wx/event.h:3671)
#11 ??			wxEvtHandler::ProcessEventLocally (this=0x94a9c28, event=...) (../../src/common/event.cpp:1522)
#12 0073EFE5	wxEvtHandler::ProcessEvent(this=0x94a9c28, event=...) (../../src/common/event.cpp:1495)
#13 0073ECCF	wxEvtHandler::SafelyProcessEvent(this=0x94a9c28, event=...) (../../src/common/event.cpp:1613)
#14 0044EF9C	wxWindowBase::HandleWindowEvent(this=this@entry=0x94a9c28, event=...) (../../src/common/wincmn.cpp:1525)
#15 00429313	wxWindow::HandleActivate(this=this@entry=0x94a9c28, state=1) (../../src/msw/window.cpp:4069)
#16 00438E86	wxWindow::MSWHandleMessage(this=0x94a9c28, result=0x29f970, message=6, wParam=1, lParam=0) (../../src/msw/window.cpp:2831)
#17 004249C0	wxWindow::MSWWindowProc(this=this@entry=0x94a9c28, message=message@entry=6, wParam=wParam@entry=1, lParam=lParam@entry=0) (../../src/msw/window.cpp:3645)
#18 00472E96	wxTopLevelWindowMSW::MSWWindowProc(this=this@entry=0x94a9c28, message=message@entry=6, wParam=wParam@entry=1, lParam=lParam@entry=0) (../../src/msw/toplevel.cpp:467)
#19 0045AF21	wxFrame::MSWWindowProc(this=0x94a9c28, message=6, wParam=1, lParam=0) (../../src/msw/frame.cpp:887)
#20 0042EA83	wxWndProc(HWND__*, unsigned int, unsigned int, long)@16(hWnd=0x11a0e6a, message=6, wParam=1, lParam=0) (../../src/msw/window.cpp:2711)
#21 745E4923	USER32!CharNextW() (C:\WINDOWS\SYSTEM32\user32.dll:??)
#22 745C4790	USER32!CallWindowProcW() (C:\WINDOWS\SYSTEM32\user32.dll:??)
#23 745C4370	USER32!DispatchMessageW() (C:\WINDOWS\SYSTEM32\user32.dll:??)
#24 745CB179	USER32!PeekMessageW() (C:\WINDOWS\SYSTEM32\user32.dll:??)
#25 7709AE46	ntdll!KiUserCallbackDispatcher() (C:\WINDOWS\SYSTEM32\ntdll.dll:??)
#26 0029FC2C	?? () (??:??)
#27 007A63D4	CallOnInit(this=<optimized out>) (../../include/wx/app.h:93)
#28 ??			wxEntryReal (argc=@0xcaf600: 1, argv=0x9499698) (../../src/common/init.cpp:479)
#29 00795015	wxEntry(argc=@0xcaf600: 1, argv=0x9499698) (../../src/msw/main.cpp:197)
Command to build wxWidgets was:

Code: Select all

mingw32-make -f makefile.gcc SHARED=0 UNICODE=1 CXXFLAGS+="-std=gnu++11 -O2 -Wno-deprecated-declarations" BUILD=release
and the same goes for BUILD=debug too.

TDM-GCC-32 5.1.0.

I pass the same cxxflags part to the sample stc project and it runs ok. In terms of the rest of the code, mine is pretty basic and using only the constructor as shown in the first post, it crashes with the above call stack. The biggest difference I see between mine and the sample is that the sample uses a class that inherits from wxStyledTextCtrl, whereas my wxStyledTextCtrl is a member of a class inheriting from wxFrame.
DenDev
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 231
Joined: Mon Jan 19, 2015 1:45 pm

Re: wxStyledTextCtrl - crash when only using constructor

Post by DenDev »

#26 in you latest stack trace suggests there is a bad pointer. Have you tried using one of the alternate MinGW's?

http://www.mingw.org/
http://sourceforge.net/projects/mingw-w64/
I have a bad habbit of not testing the code I post :D
sorinev
Knows some wx things
Knows some wx things
Posts: 40
Joined: Wed Jan 06, 2016 5:32 pm

Re: wxStyledTextCtrl - crash when only using constructor

Post by sorinev »

I downloaded MinGW 4.8.1. It surprises me that not only is the latest release 2.5 years old, but that it slipped out with that no disk in drive E bug (I seem to have a built-in card reader in my desktop that maps to 4 drives). I renamed it to K.

Anyhow, I rebuilt wxWidgets using aforementioned MinGW 4.8.1 and pointed Code::Blocks to MinGW also, and the crash is gone. So I guess there's something funky going on with TDM-GCC-32 5.1.0 that causes this. Also, the exe is smaller. 8.6MB for TDM-GCC-32 and 7.2MB for MinGW 4.8.1.
ollydbg23
Super wx Problem Solver
Super wx Problem Solver
Posts: 438
Joined: Fri Dec 12, 2008 10:31 am

Re: wxStyledTextCtrl - crash when only using constructor

Post by ollydbg23 »

From the call stack of the crash, it looks like your crash is related to this bug report, and now it should be fixed in the master header:

#17483 (wxDynamicCast segfault with mingw64 5.3.0 release build) – wxWidgets
Post Reply