Application crashing because thread is running longer - is it possible

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
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7449
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Application crashing because thread is running longer - is it possible

Post by ONEEYEMAN »

Hi, ALL,
I have an application that runs 1 thread where it connects to the DB and performs some queries.
When the application connects to the SQLite - everything works fine. No issues.
When the application connects thru the ODBC to the MS SQL Server - the application crashes about 70% of the time.

The thread is running longer when I am connected to SQL Server - that's the only difference.
I am using mutex to lock the shared data between the main (GUI) thread and my own (in both cases).
Thread do not access any GUI - it operates on the DB.
The only difference is the execution time in case of MS SQL vs SQLite.
One more thing - the crash always happens at the same place.

How to approach the debugging of such issues?

The versions:
Windows 8.1, MSVC 2017, wxWidgets 3.1.

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

Re: Application crashing because thread is running longer - is it possible

Post by doublemax »

ONEEYEMAN wrote: Fri Sep 23, 2022 3:15 pm the application crashes about 70% of the time.

the crash always happens at the same place.
That's a strange combination, but if it always crashes at the same place, then that's the place to start.

Why does it crash there? If it's a bad pointer, why is it bad? Then work your way backwards from there.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7449
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Application crashing because thread is running longer - is it possible

Post by ONEEYEMAN »

doublemax,
An assert message says "Bad HWND()... for a scrolled window"

And yes - it is strange, but this is the only difference between those 2 scenarios.

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

Re: Application crashing because thread is running longer - is it possible

Post by doublemax »

That's not much to work with.

How is the window related to the DB access? What's its lifetime? If it's supposed to exist all the time, maybe something corrupted its memory?
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7449
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Application crashing because thread is running longer - is it possible

Post by ONEEYEMAN »

doublemax,
It shuold.
But then why its not crashing/asserting for SQLite
User avatar
doublemax
Moderator
Moderator
Posts: 19102
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Application crashing because thread is running longer - is it possible

Post by doublemax »

ONEEYEMAN wrote: Fri Sep 23, 2022 6:20 pm But then why its not crashing/asserting for SQLite
That's secondary.

At the time of the crash/assert, you should have a wxWindow pointer. Is it valid? If yes, does the wxWindow data look ok?
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7449
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Application crashing because thread is running longer - is it possible

Post by ONEEYEMAN »

doblemax,
At the time of the crash the window pointer is good and valid.
Inspecting the data seems good.

And as you can see - hWnd is NULL.

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

Re: Application crashing because thread is running longer - is it possible

Post by doublemax »

I really looks as if you're calling SetScrollPos too early. Set a breakpoint in the FieldWindow ctor and single-step through the code. Either the window is not fully constructed yet, or something deletes the HWND entry at some time.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7449
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Application crashing because thread is running longer - is it possible

Post by ONEEYEMAN »

doublemax,

Code: Select all

    Create( parent, wxID_ANY, pos == wxDefaultPosition ? wxDefaultPosition : pos, wxSize( width == -1 ? parent->GetSize().GetWidth() : width, 55 ), wxBORDER_SIMPLE | wxHSCROLL | wxALWAYS_SHOW_SB );
    if( GetHwnd() )
        wxMessageBox( "Handle is valid" );
    else
        wxMessageBox( "No handle" );
    SetVirtualSize( 1000, 30 );
    if( GetHwnd() )
        wxMessageBox( "Hcoandle is valid" );
    else
        wxMessageBox( "No handle" );
    SetScrollRate( 20, 0 );
    SetCanvasColour( *wxWHITE );
Sometimes I see "Handle is valid" and sometimes "No handle".

I just added those if() / else with the wxMessageBox for debugging purposes.

Besides, with SQLite everything works every time...

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

Re: Application crashing because thread is running longer - is it possible

Post by doublemax »

I'm afraid printf-type debugging won't be enough here, You need to catch the bug in the act by single-stepping through the code.

If the handle doesn't get overwritten somehow, maybe the window construction fails. This could happen if the parent is NULL, but then the GetSize() call would crash. What's the value of "width" when you get the assert?
Besides, with SQLite everything works every time...
Yes, this is strange. Hopefully that gets explained when you find the reason for the issue.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7449
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Application crashing because thread is running longer - is it possible

Post by ONEEYEMAN »

doublemax,
On the failure the value f the width is -1.

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

Re: Application crashing because thread is running longer - is it possible

Post by doublemax »

Code: Select all

FieldWindow::FieldWindow(wxWindow *parent, int type, const wxPoint &pos, int width) : wxSFShapeCanvas()
{
    m_draggingField = nullptr;
    m_isDragging = false;
    Create( parent, wxID_ANY, pos == wxDefaultPosition ? wxDefaultPosition : pos,
              wxSize( width == -1 ? parent->GetSize().GetWidth() : width, 55 ), wxBORDER_SIMPLE | wxHSCROLL | wxALWAYS_SHOW_SB );
When you're calling Create here, you're actually calling wxWindow::Create. But i think you should call wxSFShapeCanvas::Create().

Check if that changes anything.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7449
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Application crashing because thread is running longer - is it possible

Post by ONEEYEMAN »

doublemax,
It is calling bool wxSFShapeCanvas::Create().

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

Re: Application crashing because thread is running longer - is it possible

Post by doublemax »

ONEEYEMAN wrote: Wed Sep 28, 2022 11:38 pm doublemax,
It is calling bool wxSFShapeCanvas::Create().
Yeah, sorry. Guess i wasn't fully awake yet when i looked into the sources.

It still feels like an issue that can be found by setting a breakpoint at the FieldWindow ctor and single-stepping through the code. But it's always such a pain to build your project... I wish it was monolithic without the dozens of DLLs.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7449
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Application crashing because thread is running longer - is it possible

Post by ONEEYEMAN »

doublemax,
I changed it so now it uses environment variable to locate wx.

All DLLs are building inside the executable directory. You just need to copy libmysql, libpq and VLD ones before building.

And to reproduce - install SQL Server if not already there and create a DB. Then make ODBC profile.

One other thing - an assert is not always happening...

Thank you.
Post Reply