[GRASS-dev] [GRASS GIS] #3607: Removing map layer crashes wxGUI

#3607: Removing map layer crashes wxGUI
-----------------------------------------+-------------------------
Reporter: cmbarton | Owner: grass-dev@…
     Type: defect | Status: new
Priority: normal | Milestone:
Component: wxGUI | Version: 7.4.0
Keywords: remove layer, layer manager | CPU: OSX/Intel
Platform: MacOSX |
-----------------------------------------+-------------------------
If you try to remove a raster or vector map from the layer manager, using
the "remove" option in the popup list launched by the button on the right
side of the layer, it will crash the entire GUI with the following error:

/Applications/GRASS-7.5.app/Contents/Resources/bin/pythonw: line 3: 25590
Segmentation fault: 11
/Applications/GRASS-7.5.app/Contents/Resources/python.app/Contents/MacOS/python
"$@"

This error and behavior is similar to those reported in #3472 and #3487.
This was reported to me for v. 7.4.0 but also occurs in the most recent
trunk (r73007)

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3607&gt;
GRASS GIS <https://grass.osgeo.org>

#3607: Removing map layer crashes wxGUI
------------------------+-----------------------------------------
  Reporter: cmbarton | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.4.2
Component: wxGUI | Version: 7.4.0
Resolution: | Keywords: remove layer, layer manager
       CPU: OSX/Intel | Platform: MacOSX
------------------------+-----------------------------------------
Changes (by cmbarton):

* milestone: => 7.4.2

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3607#comment:1&gt;
GRASS GIS <https://grass.osgeo.org>

#3607: Removing map layer crashes wxGUI
------------------------+-----------------------------------------
  Reporter: cmbarton | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.4.2
Component: wxGUI | Version: 7.4.0
Resolution: | Keywords: remove layer, layer manager
       CPU: OSX/Intel | Platform: MacOSX
------------------------+-----------------------------------------

Comment (by annakrat):

I can't test myself now, but this seems weird. Does this happen every
time? Do you have at the same time vector digitizer or 3D view on?

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3607#comment:2&gt;
GRASS GIS <https://grass.osgeo.org>

#3607: Removing map layer crashes wxGUI
------------------------+-----------------------------------------
  Reporter: cmbarton | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.4.2
Component: wxGUI | Version: 7.4.0
Resolution: | Keywords: remove layer, layer manager
       CPU: OSX/Intel | Platform: MacOSX
------------------------+-----------------------------------------

Comment (by cmbarton):

Every time, but only if removed in this way. All other remove methods work
fine.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3607#comment:3&gt;
GRASS GIS <https://grass.osgeo.org>

#3607: Removing map layer crashes wxGUI
------------------------+-----------------------------------------
  Reporter: cmbarton | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.4.5
Component: wxGUI | Version: 7.4.0
Resolution: | Keywords: remove layer, layer manager
       CPU: OSX/Intel | Platform: MacOSX
------------------------+-----------------------------------------

Comment (by nila):

This is still an issue with 7.8 branch and master (7.9.dev), with Python
3.8.2 and wxPython 4.0.7.post2.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3607#comment:7&gt;
GRASS GIS <https://grass.osgeo.org>

#3607: Removing map layer crashes wxGUI
------------------------+-----------------------------------------
  Reporter: cmbarton | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.4.5
Component: wxGUI | Version: 7.4.0
Resolution: | Keywords: remove layer, layer manager
       CPU: OSX/Intel | Platform: MacOSX
------------------------+-----------------------------------------

Comment (by annakrat):

Just to make it clear, do you have to press the button to crash, or also
right click (or whatever you use on mac) would do it as well?

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3607#comment:8&gt;
GRASS GIS <https://grass.osgeo.org>

#3607: Removing map layer crashes wxGUI
------------------------+-----------------------------------------
  Reporter: cmbarton | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.4.5
Component: wxGUI | Version: 7.4.0
Resolution: | Keywords: remove layer, layer manager
       CPU: OSX/Intel | Platform: MacOSX
------------------------+-----------------------------------------

Comment (by nila):

Yes, this only crashes when removing layer from button-menu.

But **not** from contextual menu (right click), where it works as
expected.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3607#comment:9&gt;
GRASS GIS <https://grass.osgeo.org>

#3607: Removing map layer crashes wxGUI
------------------------+-----------------------------------------
  Reporter: cmbarton | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.4.5
Component: wxGUI | Version: 7.4.0
Resolution: | Keywords: remove layer, layer manager
       CPU: OSX/Intel | Platform: MacOSX
------------------------+-----------------------------------------
Changes (by nila):

* Attachment "3607_debug_log_2020-03-12.txt" added.

backtrace log up to crash

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3607&gt;
GRASS GIS <https://grass.osgeo.org>

#3607: Removing map layer crashes wxGUI
------------------------+-----------------------------------------
  Reporter: cmbarton | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.4.5
Component: wxGUI | Version: 7.4.0
Resolution: | Keywords: remove layer, layer manager
       CPU: OSX/Intel | Platform: MacOSX
------------------------+-----------------------------------------

Comment (by nila):

I managed to find the cause of this bug (see attachment above). In short:
1. clicking the button triggers an event with the `GenBitmapButton`
embedded
2. the layer (`TreeItem`) is deleted (`GMFrame::OnDeleteLayer()`) and with
it the included button (the same as in the event)
3. further event handling (in `wx.lib.buttons.GenButton::OnLeftUp()`)
deals with a still living python object with a
          "wrapped C/C++ object of type `GenBitmapButton` has been deleted"

I couldn't find a quick fix for this, but I'll try to come up with a
solution.
Still, if someone more experienced is up to the challenge to address this
I'd be more than happy.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3607#comment:10&gt;
GRASS GIS <https://grass.osgeo.org>

#3607: Removing map layer crashes wxGUI
------------------------+-----------------------------------------
  Reporter: cmbarton | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.4.5
Component: wxGUI | Version: 7.4.0
Resolution: | Keywords: remove layer, layer manager
       CPU: OSX/Intel | Platform: MacOSX
------------------------+-----------------------------------------

Comment (by annakrat):

Thanks for the detailed analysis. I can't test but this might work:

{{{
diff --git a/gui/wxpython/lmgr/layertree.py
b/gui/wxpython/lmgr/layertree.py
index 52c20dcc0..b2b39c613 100644
--- a/gui/wxpython/lmgr/layertree.py
+++ b/gui/wxpython/lmgr/layertree.py
@@ -421,7 +421,7 @@ class LayerTree(treemixin.DragAndDrop,
CT.CustomTreeCtrl):
              # select the layer in the same way as right click
              if not self.IsSelected(layer):
                  self.DoSelectItem(layer, True, False)
- self.OnLayerContextMenu(event)
+ wx.CallAfter(self.OnLayerContextMenu, event)

}}}

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3607#comment:11&gt;
GRASS GIS <https://grass.osgeo.org>

#3607: Removing map layer crashes wxGUI
------------------------+-----------------------------------------
  Reporter: cmbarton | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.8.3
Component: wxGUI | Version: git-releasebranch78
Resolution: | Keywords: remove layer, layer manager
       CPU: OSX/Intel | Platform: MacOSX
------------------------+-----------------------------------------
Changes (by neteler):

* version: 7.4.0 => git-releasebranch78
* milestone: 7.4.5 => 7.8.3

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3607#comment:12&gt;
GRASS GIS <https://grass.osgeo.org>

#3607: Removing map layer crashes wxGUI
------------------------+-----------------------------------------
  Reporter: cmbarton | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.8.3
Component: wxGUI | Version: git-releasebranch78
Resolution: | Keywords: remove layer, layer manager
       CPU: OSX/Intel | Platform: MacOSX
------------------------+-----------------------------------------

Comment (by nila):

Replying to [comment:11 annakrat]:
> Thanks for the detailed analysis. I can't test but this might work:
>
>
> {{{
> diff --git a/gui/wxpython/lmgr/layertree.py
b/gui/wxpython/lmgr/layertree.py
> index 52c20dcc0..b2b39c613 100644
> --- a/gui/wxpython/lmgr/layertree.py
> +++ b/gui/wxpython/lmgr/layertree.py
> @@ -421,7 +421,7 @@ class LayerTree(treemixin.DragAndDrop,
CT.CustomTreeCtrl):
> # select the layer in the same way as right click
> if not self.IsSelected(layer):
> self.DoSelectItem(layer, True, False)
> - self.OnLayerContextMenu(event)
> + wx.CallAfter(self.OnLayerContextMenu, event)
>
> }}}

Brilliant, it works fine, thanks!

Will you put up a PR?

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3607#comment:13&gt;
GRASS GIS <https://grass.osgeo.org>

#3607: Removing map layer crashes wxGUI
------------------------+-----------------------------------------
  Reporter: cmbarton | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.8.3
Component: wxGUI | Version: git-releasebranch78
Resolution: | Keywords: remove layer, layer manager
       CPU: OSX/Intel | Platform: MacOSX
------------------------+-----------------------------------------

Comment (by annakrat):

Sure: https://github.com/OSGeo/grass/pull/462

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3607#comment:14&gt;
GRASS GIS <https://grass.osgeo.org>

#3607: Removing map layer crashes wxGUI
------------------------+-----------------------------------------
  Reporter: cmbarton | Owner: grass-dev@…
      Type: defect | Status: closed
  Priority: normal | Milestone: 7.8.3
Component: wxGUI | Version: git-releasebranch78
Resolution: fixed | Keywords: remove layer, layer manager
       CPU: OSX/Intel | Platform: MacOSX
------------------------+-----------------------------------------
Changes (by annakrat):

* status: new => closed
* resolution: => fixed

Comment:

Merged to master and 78.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3607#comment:15&gt;
GRASS GIS <https://grass.osgeo.org>