Wxwidget 2.8 - wxPostEvent crashes

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
cminus
In need of some credit
In need of some credit
Posts: 9
Joined: Mon May 28, 2018 2:15 pm

Wxwidget 2.8 - wxPostEvent crashes

Post by cminus »

[*]I've a huge multi-threaded app that utilizes events to update the GUI.
So with a custom Event, wxPostEvent crashes with the following log :

Code: Select all

#0  0x00007ffff5464885 in raise () from /lib64/libc.so.6
#1  0x00007ffff5465e61 in abort () from /lib64/libc.so.6
#2  0x00007ffff54a587f in __libc_message () from /lib64/libc.so.6
#3  0x00007ffff54ab088 in malloc_printerr () from /lib64/libc.so.6
#4  0x00007ffff54ae6cd in _int_malloc () from /lib64/libc.so.6
#5  0x00007ffff54b01b7 in malloc () from /lib64/libc.so.6
#6  0x00007ffff5eb806d in operator new (sz=48) at ../../../../libstdc++-v3/libsupc++/new_op.cc:52
#7  0x000000000056ab4b in wxObjectList::CreateNode(wxNodeBase*, wxNodeBase*, void*, wxListKey const&) ()
#8  0x00007ffff706ed9e in wxListBase::Append(void*) () from /tools/common/external/wxWidgets2.8.10/lib/libwx_gtk2-2.8.so.0
#9  0x00007ffff70bd8bd in wxEvtHandler::AddPendingEvent(wxEvent&) () from /tools/common/external/wxWidgets2.8.10/lib/libwx_gtk2-2.8.so.0
#10 0x00000000004d8201 in BaseMainFrame::SendMessageEventToUser(MessageType, std::string, std::string, FontColour, bool, bool) ()

The code firing the event

Code: Select all


	CustomGUIMessageEvent p_event(EVENT_MESSAGE, ID_ERROR_MESSAGE);
	p_event.SetMessage(ErrorMessage);
	p_event.SetMessageTitle(Title);
	wxPostEvent(this, p_event);
Where ErrorMessage and Title are standard C++ string

Notes:
* Platform : Linux, Compiler : g++ 4.7, Wxwidgets : 2.8.10
* The error is not easily reproducible - I have to leave the setup for a few hours - But it's always the same backtrace.

Trials of debug :
1. Adding Mutex on the wxPostEvent calls on the same target, trying to synchronize event posting myself instead of depending on wxPostEvent being thread-safe ( fails )
2. Investigating custom event internal members and disabling COW (Copy-On Write ) for stl strings by providing a copy constructor ( fails )
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Wxwidget 2.8 - wxPostEvent crashes

Post by doublemax »

Did you override the Clone() method in CustomGUIMessageEvent?

If no: You have to. And make sure to create deep copies of the strings.

If yes: Please show the code.
Use the source, Luke!
cminus
In need of some credit
In need of some credit
Posts: 9
Joined: Mon May 28, 2018 2:15 pm

Re: Wxwidget 2.8 - wxPostEvent crashes

Post by cminus »

doublemax wrote:Did you override the Clone() method in CustomGUIMessageEvent?

If no: You have to. And make sure to create deep copies of the strings.

If yes: Please show the code.
Yes I did,

Code: Select all

#ifndef CUSTOM_GUI_MESSAGE_EVENT
#define CUSTOM_GUI_MESSAGE_EVENT

#include <wx/event.h>

class CustomGUIMessageEvent: public wxNotifyEvent
{
public:
	CustomGUIMessageEvent( wxEventType commandType = wxEVT_NULL, int id = 0 );

	// accessors
	string GetMessage(){return m_strMessage;}

	string GetMessageTitle(){return m_strMessageTitle;}


	void SetMessage(string p_strMessage){m_strMessage = p_strMessage;}

	void SetMessageTitle(string p_strMessageTitle){m_strMessageTitle = p_strMessageTitle;}


	// required for sending with wxPostEvent()
	wxEvent* Clone()const { return new CustomGUIMessageEvent(*this);};

private:
	string m_strMessage;
	string m_strMessageTitle;

};

typedef void (wxEvtHandler::*CustomGUIMessageEventFunction)(CustomGUIMessageEvent&);

DECLARE_EVENT_TYPE( EVENT_MESSAGE, 1000 )


#define EVT_MESSAGE(id, fn) \
	DECLARE_EVENT_TABLE_ENTRY( EVENT_MESSAGE, id, -1, \
	(wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) (wxNotifyEventFunction) \
	wxStaticCastEvent( CustomGUIMessageEventFunction, & fn ), (wxObject *) NULL ),

#endif CUSTOM_GUI_MESSAGE_EVENT


User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Wxwidget 2.8 - wxPostEvent crashes

Post by doublemax »

Ok, that code looks fine.

I checked the code for AddPendingEvent() in wx 2.8.12, the list of pending events is protected with a critical section, so that should be safe, too.

As the assert happens in malloc(), is it possible that you just run out of memory? Did you check the memory usage of your application?
If that's not it, i'm out of ideas.

Are you stuck with wx 2.8.x or could you try to upgrade to 3.1.x?
Use the source, Luke!
cminus
In need of some credit
In need of some credit
Posts: 9
Joined: Mon May 28, 2018 2:15 pm

Re: Wxwidget 2.8 - wxPostEvent crashes

Post by cminus »

doublemax wrote:Ok, that code looks fine.

I checked the code for AddPendingEvent() in wx 2.8.12, the list of pending events is protected with a critical section, so that should be safe, too.

As the assert happens in malloc(), is it possible that you just run out of memory? Did you check the memory usage of your application?
If that's not it, i'm out of ideas.
I have a lot of memory on the server about 230G free memory and the crash occurs in a stochastic
Are you stuck with wx 2.8.x or could you try to upgrade to 3.1.x?
Yup I am stuck with wx 2.8, the code base is large to make a swift upgrade to wx 3.1 :(

Thanks :D
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Wxwidget 2.8 - wxPostEvent crashes

Post by doublemax »

I don't work under Linux, so i can't read any more from this call stack.

Try asking on the wx-users group, maybe someone there has an idea:
https://groups.google.com/forum/#!forum/wx-users
Use the source, Luke!
DavidHart
Site Admin
Site Admin
Posts: 4252
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Re: Wxwidget 2.8 - wxPostEvent crashes

Post by DavidHart »

Hi,
2. Investigating custom event internal members and disabling COW (Copy-On Write ) for stl strings by providing a copy constructor ( fails )
The missing copy ctor was the first thing I saw. Are you certain you did a deep copy of the strings inside it?

What happens if you temporarily remove the strings, or replace them with ints? Does it still crash?

Regards,

David
cminus
In need of some credit
In need of some credit
Posts: 9
Joined: Mon May 28, 2018 2:15 pm

Re: Wxwidget 2.8 - wxPostEvent crashes

Post by cminus »

@DavidHart, Here is the original copy constructor

Code: Select all

CustomGUIMessageEvent::CustomGUIMessageEvent(const CustomGUIMessageEvent& rOther)
: wxNotifyEvent(rOther)
, m_strMessage(rOther.m_strMessage)
, m_strMessageTitle(rOther.m_strMessageTitle)
{
}
I made the following trials
1. replacing `m_strMessage(rOther.m_strMessage)` with `m_strMessage(rOther.m_strMessage.c_str()))`, to my knowledge this disables COW -> crashed
2. Commented out copying the strings -> didn't crash till now ( running for 4 days & 220K operations )
3. replacing C++ strings with C string utilizing `strcpy` and add length variables -> didn't crash till now ( running for a day and 74K operations)

On average it crashes after a day and half or after 80K operation.

So I believe it's memory corruption caused by the C++ string.. But still I have no idea why this corruption happens.

N.B : I will update the threads a few days later to confirm my theory.

------
Update :
Second trial ran for 5 days & 270K operations then I closed it by myself.
Third trial is running for 2 days till now and 160K operations.

So I am sure that it's a memory corruption issue with C++ strings under GCC/4.74.
I will try to investigate how this happens.
Last edited by cminus on Tue Jun 05, 2018 9:13 am, edited 1 time in total.
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: Wxwidget 2.8 - wxPostEvent crashes

Post by eranon »

Hello, Read in diagonale, but what if you allocate the event on heap and use QueueEvent?

EDIT: Read you last message above I zapped in a first pass... Well, yes, the use of c_str() is the deep copy doublemax talked about.
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Wxwidget 2.8 - wxPostEvent crashes

Post by doublemax »

Did you also use the deep copy in the SetMessage* methods?

Can you find out which message malloc_printerr() is trying to print?
Use the source, Luke!
cminus
In need of some credit
In need of some credit
Posts: 9
Joined: Mon May 28, 2018 2:15 pm

Re: Wxwidget 2.8 - wxPostEvent crashes

Post by cminus »

doublemax wrote:Did you also use the deep copy in the SetMessage* methods?
In case of using C++ strings, the answer is No.
But currently with C-strings, yes.

I will make a trial of C++ strings with deep copying in `SetMessage` and Custom Copy Constructor.
Can you find out which message malloc_printerr() is trying to print?
Sorry, but how is this possible ?
Note : I only have release build of libc.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Wxwidget 2.8 - wxPostEvent crashes

Post by ONEEYEMAN »

Hi,
You have a wxListBase::Append() in you backtrace.
Can you find out where this call is made? And whether it is succeeded or not? Also, if the program is crashing it means that something is using the NULL pointer.
Just check what parameter you are setting as NULL when you create your own event.

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Wxwidget 2.8 - wxPostEvent crashes

Post by doublemax »

You have a wxListBase::Append() in you backtrace.
Can you find out where this call is made? And whether it is succeeded or not?
That's inside AddPendingEvent().
Also, if the program is crashing it means that something is using the NULL pointer.
There are many other reasons why a program can crash.

Based on the backtrace it seems clear that malloc() raises an exception.

Looking at the glib source, there are a few occasions where malloc() calls malloc_printerr() with an error message. It would have been nice to know which one it was in this case.
https://sourceware.org/git/?p=glibc.git ... 9b22#l3523
Use the source, Luke!
cminus
In need of some credit
In need of some credit
Posts: 9
Joined: Mon May 28, 2018 2:15 pm

Re: Wxwidget 2.8 - wxPostEvent crashes

Post by cminus »

doublemax wrote: Looking at the glib source, there are a few occasions where malloc() calls malloc_printerr() with an error message. It would have been nice to know which one it was in this case.
https://sourceware.org/git/?p=glibc.git ... 9b22#l3523
I looked at the link provided above and after few looking, I found out:
1. malloc_printerr put: malloc(): memory corruption (fast): 0x00007fffa81a502f to terminal
2. printing only did happen in debug build, but not on release builds. I believe they are disabled at release ( not sure by default or something in these vast make files)

Considering the last trial I do, that's using deep copying on C++ strings in setters and event copy constructor, it reached 89K operations without crashing and I believe it won't.

I know believe it's an issue caused by COW feature of c++ strings, also I guess the reason is as following :
As a different thread creates the string and passes it to setMessage, then another thread consumes the event. And C++ strings under gcc 4.7 is not thread safe.

Not sure even if this conclusion is true, but I will stop investigating at this level.

Really deep thanks for support.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Wxwidget 2.8 - wxPostEvent crashes

Post by ONEEYEMAN »

Hi,
You can try to upgrade the compiler - this should be much easier than trying to fix all those wx incompatibilities. ;-)

Thank you.
Post Reply