What is the right way to close a frame in wxWidgets Topic is solved

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.
sly_chandan
Earned some good credits
Earned some good credits
Posts: 116
Joined: Fri Sep 25, 2015 2:33 pm

What is the right way to close a frame in wxWidgets

Post by sly_chandan »

I have created an application that has a ShellFromFBFrame inheriting from wxFrame window.

The App object is defined as follows:

Code: Select all

bool ShellFromFBApp::OnInit()
{
    //(*AppInitialize
    bool wxsOK = true;
    wxInitAllImageHandlers();
    if ( wxsOK )
    {
    	ShellFromFBFrame* Frame = new ShellFromFBFrame(0);
    	Frame->Show();
    }
    //*)
    return wxsOK;

}
The ShellFromFBFrame is as follows:

Code: Select all

ShellFromFBFrame::ShellFromFBFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(ShellFromFBFrame)
    wxBoxSizer* MainSizer;
    wxBoxSizer* mainContentSizer;
    wxMenu* createContact;
...

the ShellFromFBFrame opens a new CreateContactFrame:

Code: Select all

void ShellFromFBFrame::OnCreateContact(wxCommandEvent& event)
{
    CreateContactFrame* createContactFrame = new CreateContactFrame(NULL,wxID_ANY);
    createContactFrame->Show(true);
}

The CreateContactFrame is as follows:

Code: Select all

CreateContactFrame::CreateContactFrame(wxWindow* parent,wxWindowID id)
{
    //ctor
    Create(parent, id, wxT("Create Contact"), wxDefaultPosition, wxSize(1100,700), wxDEFAULT_FRAME_STYLE, _T("id"));
//    int num;
//    num = wxAtoi(row);
    //this->rowToFetch = row;
...

BEGIN_EVENT_TABLE(CreateContactFrame, wxFrame)
EVT_BUTTON(ID_CREATEBTN, CreateContactFrame::CreateContact)
EVT_CLOSE(CreateContactFrame::OnClose)
EVT_BUTTON(wxID_CANCEL,CreateContactFrame::OnCancel)
END_EVENT_TABLE()


void CreateContactFrame::OnClose(wxCloseEvent& WXUNUSED(event))
{
    this->Close();
}


void CreateContactFrame::OnCancel(wxCommandEvent& WXUNUSED(event))
{
    this->Close();
}
But when I close the CreateContactFrame window by close button or cancel button. My App crashes and I get the following process terminated error message in build logs:

Process terminated with status -1073740940 (0 minute(s), 12 second(s))

What am I doing wrong?
Last edited by doublemax on Thu Feb 18, 2021 9:56 am, edited 1 time in total.
Reason: Added code tags
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: What is the right way to close a frame in wxWidgets

Post by doublemax »

Code: Select all

void CreateContactFrame::OnClose(wxCloseEvent& WXUNUSED(event))
{
this->Close();
}
Calling Close() triggers the EVT_CLOSE event, so you're getting an endless recursion here. Just call Destroy(), or don't have an EVT_CLOSE handler at all. The default behavior of a wxFrame is to destroy itself when it gets closed.

It's still ok to call Close() in the OnCancel method.

BTW: CreateContactFrame should probably be a modal wxDialog instead of a (non-modal) wxFrame.
Use the source, Luke!
sly_chandan
Earned some good credits
Earned some good credits
Posts: 116
Joined: Fri Sep 25, 2015 2:33 pm

Re: What is the right way to close a frame in wxWidgets

Post by sly_chandan »

I have removed the EVT_CLOSE and the event handler

I have now used this->Close(); for the OnCancel button.

The Destructor goes on infinite recursion.

I am still missing something here.

The Debugger gives the following Error:

Active debugger config: GDB/CDB debugger:Default
Building to ensure sources are up-to-date
Selecting target:
Debug
Adding source dir: D:\Programming\MySalesAppWxWidgets\ShellFromFB\
Adding source dir: D:\Programming\MySalesAppWxWidgets\ShellFromFB\
Adding file: D:\Programming\MySalesAppWxWidgets\ShellFromFB\bin\Debug\ShellFromFB.exe
Changing directory to: D:/Programming/MySalesAppWxWidgets/ShellFromFB/.
Set variable: PATH=.;F:\wxWidgets-3.1.4\lib\gcc_dll;F:\TDM-GCC-64\bin;F:\TDM-GCC-64;C:\Program Files\Common Files\Oracle\Java\javapath;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Microsoft DirectX SDK (February 2006)\Utilities\Bin\x86;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files\dotnet;C:\Program Files\Microsoft SQL Server\130\Tools\Binn;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn;C:\Program Files (x86)\Calibre2;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn;C:\Program Files\Microsoft SQL Server\120\Tools\Binn;C:\Program Files\Microsoft SQL Server\120\DTS\Binn;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio;C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn;C:\Program Files\Git\cmd;C:\Program Files\PostgreSQL\11\bin;C:\Python;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit;C:\Program Files\Common Files\Autodesk Shared;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.22.27905\bin\Hostx86\x86;D:\Softwares\mingw-w64-v7.0.0\mingw-w64-v7.0.0;C:\Program Files\Microsoft Windows Performance Toolkit;D:\Softwares\mingw-w64-v7.0.0;C:\Program Files (x86)\dotnet;C:\Program Files\nodejs;C:\Program Files\Java\jdk-15.0.2\bin;C:\Users\CHANDAN\AppData\Local\Programs\Python\Python39\Scripts;C:\Users\CHANDAN\AppData\Local\Programs\Python\Python39;C:\Users\CHANDAN\AppData\Roaming\npm;C:\Users\CHANDAN\AppData\Roaming\npm\node_modules\@angular\cli\bin;C:\Users\CHANDAN\AppData\Local\Programs\Microsoft VS Code\bin;C:\Program Files\Java\jdk-9.0.4\bin;F:\wxWidgets-3.1.4\lib\gcc_dll;C:\Program Files\CodeBlocks\MinGW\bin
Starting debugger: C:\Program Files\CodeBlocks\MINGW\bin\gdb.exe -nx -fullname -quiet -args D:/Programming/MySalesAppWxWidgets/ShellFromFB/bin/Debug/ShellFromFB.exe
done
Setting breakpoints
Debugger name and version: GNU gdb (GDB) 8.1
Child process PID: 5428
At D:\Programming\MySalesAppWxWidgets\ShellFromFB\ViewContactsFrame2.cpp:1166
At D:\Programming\MySalesAppWxWidgets\ShellFromFB\ViewContactsFrame2.cpp:1168
In wxAppConsoleBase::CallEventHandler(wxEvtHandler*, wxEventFunctor&, wxEvent&) const () (F:\wxWidgets-3.1.4\lib\gcc_dll\wxmsw314u_gcc_custom.dll)
In wxEvtHandler::ProcessEventIfMatchesId(wxEventTableEntryBase const&, wxEvtHandler*, wxEvent&) () (F:\wxWidgets-3.1.4\lib\gcc_dll\wxmsw314u_gcc_custom.dll)
#29 0x000000000040db78 in WinMain (hInstance=0x400000, hPrevInstance=0x0, nCmdShow=10) at D:\Programming\MySalesAppWxWidgets\ShellFromFB\ShellFromFBApp.cpp:17
D:\Programming\MySalesAppWxWidgets\ShellFromFB\ShellFromFBApp.cpp:17:395:beg:0x40db78
At D:\Programming\MySalesAppWxWidgets\ShellFromFB\ShellFromFBApp.cpp:17
In wxEventHashTable::HandleEvent(wxEvent&, wxEvtHandler*) () (F:\wxWidgets-3.1.4\lib\gcc_dll\wxmsw314u_gcc_custom.dll)
#28 0x000000000040db78 in WinMain (hInstance=0x400000, hPrevInstance=0x0, nCmdShow=10) at D:\Programming\MySalesAppWxWidgets\ShellFromFB\ShellFromFBApp.cpp:17
D:\Programming\MySalesAppWxWidgets\ShellFromFB\ShellFromFBApp.cpp:17:395:beg:0x40db78
At D:\Programming\MySalesAppWxWidgets\ShellFromFB\ShellFromFBApp.cpp:17
In wxEvtHandler::TryHereOnly(wxEvent&) () (F:\wxWidgets-3.1.4\lib\gcc_dll\wxmsw314u_gcc_custom.dll)
#27 0x000000000040db78 in WinMain (hInstance=0x400000, hPrevInstance=0x0, nCmdShow=10) at D:\Programming\MySalesAppWxWidgets\ShellFromFB\ShellFromFBApp.cpp:17
D:\Programming\MySalesAppWxWidgets\ShellFromFB\ShellFromFBApp.cpp:17:395:beg:0x40db78
At D:\Programming\MySalesAppWxWidgets\ShellFromFB\ShellFromFBApp.cpp:17
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: What is the right way to close a frame in wxWidgets

Post by doublemax »

I can't think of any other reason why it would crash. Does the other frame close fine?

Code: Select all

ShellFromFBApp.cpp:17
What's at line 17 of that file?
Use the source, Luke!
sly_chandan
Earned some good credits
Earned some good credits
Posts: 116
Joined: Fri Sep 25, 2015 2:33 pm

Re: What is the right way to close a frame in wxWidgets

Post by sly_chandan »

The Line contains: IMPLEMENT_APP(ShellFromFBApp);

I get this at the debugger being called recursively
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: What is the right way to close a frame in wxWidgets

Post by doublemax »

Code: Select all

At D:\Programming\MySalesAppWxWidgets\ShellFromFB\ViewContactsFrame2.cpp:1166
At D:\Programming\MySalesAppWxWidgets\ShellFromFB\ViewContactsFrame2.cpp:1168
What's the code around these lines?
Use the source, Luke!
sly_chandan
Earned some good credits
Earned some good credits
Posts: 116
Joined: Fri Sep 25, 2015 2:33 pm

Re: What is the right way to close a frame in wxWidgets

Post by sly_chandan »

this->Close();


When I click the Close button of the frame the destructor is being called recursively infinitely

I have a similar functionality for another form and it works fine.

It is just for this frame it doesnt work.

I have noticed now. Even the EVT_CLOSE is being called recursively.

What if I revert to a previous version of wxwidgets? Could this be a problem with the wxwidgets library?
Last edited by sly_chandan on Thu Feb 18, 2021 11:31 am, edited 1 time in total.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: What is the right way to close a frame in wxWidgets

Post by doublemax »

And you really have no EVT_CLOSE event handler any more? Very strange. What happens if you call Destroy() instead?
Use the source, Luke!
sly_chandan
Earned some good credits
Earned some good credits
Posts: 116
Joined: Fri Sep 25, 2015 2:33 pm

Re: What is the right way to close a frame in wxWidgets

Post by sly_chandan »

I have tried even the EVT_CLOSE and the Destructor is being called infinitely recursively.
Even the Destroy() function wont do.
What if I return to the previous version of wxWidgets?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: What is the right way to close a frame in wxWidgets

Post by doublemax »

I don't think it has anything to do with the wxWidgets version. It's most likely something very simple.

Can you post the whole code somewhere? If not, try to create a minimal compilable sample that shows the issue.
Use the source, Luke!
sly_chandan
Earned some good credits
Earned some good credits
Posts: 116
Joined: Fri Sep 25, 2015 2:33 pm

Re: What is the right way to close a frame in wxWidgets

Post by sly_chandan »

It works fine for another form that I have coded which is ViewTacticsFrame

With just this form and a second form which are ViewContactsFrame and CreateContactFrame there is a problem.


There is No difference in the forms close functionality.

The only difference is that tactics frame just has fewer controls than the 2 forms.

Is there a memory leak problem because the 2 forms are heavy on controls?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: What is the right way to close a frame in wxWidgets

Post by doublemax »

sly_chandan wrote: Thu Feb 18, 2021 11:52 am Is there a memory leak problem because the 2 forms are heavy on controls?
Very unlikely. How many controls are you talking about? As long as you're not hitting the 10000 GDI handle limit under Windows, it shouldn't be a problem.
Use the source, Luke!
sly_chandan
Earned some good credits
Earned some good credits
Posts: 116
Joined: Fri Sep 25, 2015 2:33 pm

Re: What is the right way to close a frame in wxWidgets

Post by sly_chandan »

I have copied the project files of one wxwidgets compiled in release version and linked the files with the source code to another wxwidgets compiled in debug.


Could that be a problem?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: What is the right way to close a frame in wxWidgets

Post by doublemax »

sly_chandan wrote: Thu Feb 18, 2021 12:00 pm I have copied the project files of one wxwidgets compiled in release version and linked the files with the source code to another wxwidgets compiled in debug.


Could that be a problem?
Possible. I would definitely try using consistent settings across all parts of the project.
Use the source, Luke!
sly_chandan
Earned some good credits
Earned some good credits
Posts: 116
Joined: Fri Sep 25, 2015 2:33 pm

Re: What is the right way to close a frame in wxWidgets

Post by sly_chandan »

I have tried everything now buddy.


What do you suggest I should do?
Last edited by sly_chandan on Thu Feb 18, 2021 1:06 pm, edited 3 times in total.
Post Reply