Author Topic: Crash on CB exit  (Read 2704 times)

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2317
Crash on CB exit
« on: October 03, 2021, 02:47:02 am »
CB (on Windows) is crashing when exited when any plugin is dependent on its OnRelease() call to exit properly. Especially when there are floating windows open.

This happens because:
Main.cpp 3099 issues
Code
    Manager::SetAppShuttingDown(true);

    Manager::Get()->GetLogManager()->DebugLog(_T("Deinitializing plugins..."));
    CodeBlocksEvent evtShutdown(cbEVT_APP_START_SHUTDOWN);
    Manager::Get()->ProcessEvent(evtShutdown);

but Manager.cpp:256 returns immediately because of
Code
bool Manager::ProcessEvent(CodeBlocksEvent& event)
{
    if (IsAppShuttingDown())
        return false;

thus no plugin ever sees the event and no onRelease() is ever called.

I suggest manager.cpp:256 be changed to
Code
    if (IsAppShuttingDown() and (event.GetEventType() != cbEVT_APP_START_SHUTDOWN))
        return false;

My tests show this mod works.

To experience the crash, set CodeCompletions Symbols window to floating.
Pull the winidow outside CB's main window. Resize it. Now close CB.
« Last Edit: October 03, 2021, 05:06:46 am by Pecan »

Offline AndrewCot

  • Almost regular
  • **
  • Posts: 241
Re: Crash on CB exit
« Reply #1 on: October 03, 2021, 02:55:21 am »
Do you think this will fix ticket 1108?

    https://sourceforge.net/p/codeblocks/tickets/1108/

Let me know if you think it will fix it and I will give it a go over the next few days and let you know how I go.

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2317
Re: Crash on CB exit
« Reply #2 on: October 03, 2021, 05:04:44 am »
Do you think this will fix ticket 1108?

    https://sourceforge.net/p/codeblocks/tickets/1108/

Let me know if you think it will fix it and I will give it a go over the next few days and let you know how I go.

Yes, that ticket is a perfect description of my experience.

By the way... Thank you for your work.
« Last Edit: October 03, 2021, 05:08:24 am by Pecan »

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2317
Re: Crash on CB exit
« Reply #3 on: October 03, 2021, 06:18:39 pm »
I no longer believe this is a proper fix for the problem.
I can still get that OnRelease() is never called on exit if I move the floating window around (or blink twice) .

OnRelease() is called in MainFrame::OnApplicationClose(wxCloseEvent& event) by  the call to "Manager::Shutdown();", but the crash is happening way before at the "Hide();" statement;

ie., CB is attempting to Hide() the windows before OnRelease() is ever called.
That means plugins MUST close their floating windows on receiving the cbEVT_APP_START_SHUTDOWN event rather than OnRelease() .
 
There's some weird condition such that if you never touch a visible floating window, Hide() works ok. But if you ever move/resize/work-inside-of the window, Hide() crashes.
 
« Last Edit: October 03, 2021, 06:42:52 pm by Pecan »

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2317
Re: Crash on CB exit
« Reply #4 on: October 05, 2021, 03:14:43 am »
@ AndrewCot

After a full day and a half, I think I've finally found a fix for these crashes.
Try this patch.

The result is that (on Windows):
1)Hide() cannot be called when there are shown/floating windows. It hangs.
2) And shown/floating windows must be closed before calling Manager::Shutdown(); else it crashes.

This fixes my floating Symbols window crash and also fixes Bug #1108 .
 
Code
Index: src/src/main.cpp
===================================================================
--- src/src/main.cpp (revision 12516)
+++ src/src/main.cpp (working copy)
@@ -3096,6 +3096,7 @@
 
     Manager::SetAppShuttingDown(true);
 
+    // This does not call OnRelease() for the plugins. It's just a notify...
     Manager::Get()->GetLogManager()->DebugLog(_T("Deinitializing plugins..."));
     CodeBlocksEvent evtShutdown(cbEVT_APP_START_SHUTDOWN);
     Manager::Get()->ProcessEvent(evtShutdown);
@@ -3117,8 +3118,19 @@
     while (GetEventHandler() != this)
         PopEventHandler(false);
 
+     #if   defined ( __WIN32__ ) || defined ( _WIN64 )
+    // Hide() causes crashes and hangs on windows when floating windows are shown //(ph 2021/10/4)
     // Hide the window
+    //-Hide();
+    // Move the window off the screen instead of Hide()
+    wxDisplay display(wxDisplay::GetFromWindow(this));
+    wxRect screen = display.GetClientArea();
+    wxWindow* pWindow = Manager::Get()->GetAppWindow();
+    pWindow->Move(screen.GetX()+screen.GetWidth(), screen.GetY());
+    #else
+    // Hide the window
     Hide();
+    #endif
 
     if (!Manager::IsBatchBuild())
     {
@@ -3139,6 +3151,17 @@
         }
     }
 
+    #if   defined ( __WIN32__ ) || defined ( _WIN64 )
+    // Close shown floating windows before shutdown or get occasional crashes
+    wxAuiPaneInfoArray& all_panes = m_LayoutManager.GetAllPanes();
+    for(size_t ii = 0; ii < all_panes.Count(); ++ii)
+    {
+        wxAuiPaneInfo paneInfo = all_panes[ii];
+        if (paneInfo.IsShown() and paneInfo.IsFloating())
+            m_LayoutManager.ClosePane(paneInfo);
+    }
+    #endif
+
     Manager::Shutdown(); // Shutdown() is not Free(), Manager is automatically destroyed at exit
 
     Destroy();

Offline AndrewCot

  • Almost regular
  • **
  • Posts: 241
Re: Crash on CB exit
« Reply #5 on: October 06, 2021, 05:44:41 am »
@Pecan

Thanks, this fixes ticket #1108.


I will update my unofficial SF installer downloads within the next hour.

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2317
Re: Crash on CB exit
« Reply #6 on: October 06, 2021, 06:33:05 am »
@Pecan

Thanks, this fixes ticket #1108.


I will update my unofficial SF installer downloads within the next hour.

Good. I'll make the fix to head and close ticket #1108
Thanks for your help.

Offline AndrewCot

  • Almost regular
  • **
  • Posts: 241
Re: Crash on CB exit
« Reply #7 on: October 07, 2021, 12:21:08 am »
@Pecan,

It looks like something went wrong when the patch was applied.

The https://sourceforge.net/p/codeblocks/code/12533/ change is different to the patch file as it only has the second block of changes, but is in the area where the first block was changed....

Is this expected or did something go wrong?

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2317
Re: Crash on CB exit
« Reply #8 on: October 07, 2021, 07:18:55 am »
@Pecan,

It looks like something went wrong when the patch was applied.

The https://sourceforge.net/p/codeblocks/code/12533/ change is different to the patch file as it only has the second block of changes, but is in the area where the first block was changed....

Is this expected or did something go wrong?

Yes, I changed the commit code. I found that if I closed all the shown/floating windows BEFORE the Hide() call that I didn't need the move window code. And Hide() would work ok.

Thanks.