crash by removeObject in combination wih snapper

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Chri1
Posts: 86
Joined: Wed Oct 17, 2018 9:00 am

crash by removeObject in combination wih snapper

Post by Chri1 »

Hi

Following code causes crash of FreeCAD(0.18), when line with removeObject is executed - Therefore I have it commented:
(This is not the "real" code - I extracted it to show the problem.)

Code: Select all

objectAnn=None
import DraftSnap

class Ui_Dialog:
    def start():
        snapit(0)

    def cb(point):
        print("cb called by Snapper", point)

        if point.__class__.__name__ == 'Vector':
            print("Snapper clicked")
            objectAnn.LabelText=["New Text"]  #This works
#           App.ActiveDocument.removeObject(objectAnn.Label)  #THIS MAKES CRASH !!!!!!!!!!!!!!!!!!!!!!1
            FreeCAD.ActiveDocument.recompute()
        return(print("End cb"))
   
def snapit(i):
          global objectAnn
          objectAnn = App.ActiveDocument.addObject("App::AnnotationLabel","FCInfoToMouse")
          objectAnn.LabelText=["Einfügepunkt klicken"]  
          point = FreeCADGui.Snapper.getPoint(callback = Ui_Dialog.cb)   #snapit runs through and does not wait here till clicked
          print("Line after Snapper",objectAnn.Label)  

what = Ui_Dialog
what.start()
The error is
Unhandled Base::Exception caught in GUIApplication::notify.
The error message is: Access violation
Unhandled Base::Exception caught in GUIApplication::notify.
The error message is: Access violation
I tried it on two Intel-PC's : Win10 with Intel(R) UHD Graphics 620; Win7 with Nvidia GeForce GT 540M)
and an AMD-PC(Win 7,AMD-RadeonR7), always same error.

Is there a solution?
Chri1
Posts: 86
Joined: Wed Oct 17, 2018 9:00 am

Re: crash by removeObject in combination wih snapper

Post by Chri1 »

I tried same, but instead of AnnotationLabel this:

Code: Select all

sg = Gui.ActiveDocument.ActiveView.getSceneGraph()
...
te = coin.SoText2()
...
Same result, it crashed

And the error is a little different:
Unhandled Base::Exception caught in GUIApplication::notify.
The error message is: Access violation
Unhandled Base::Exception caught when notifying observer.
The error message is: Access violation


And suddenly it did not crash anymore! What happened?
New start of FreeCAD, from python console I tried some things with SceneGraph:
When I make sg.removeAllChildren() and after this I start the macro, it does not crash
But rejoiced to soon: Snapper does not respond to click, it only finishes by Cancel-Button
User avatar
Joel_graff
Veteran
Posts: 1949
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: crash by removeObject in combination wih snapper

Post by Joel_graff »

Chri1 wrote: Fri Apr 17, 2020 3:55 pm And suddenly it did not crash anymore! What happened?
This is a wild guess, but I suspect it has to do with corrupting the scenegraph during traversal. Coin3D doesn't like it when you change the scenegraph while it's traversing it to render what's onscreen. So changing it can (but not always) cause errors, segfaults, etc.

To avoid that, I suggest using DraftGui.todo. This is a function that uses QT to delay execution of a function for a moment when the UI is not being updated. It does a great job of keeping functions which modify the scenegraph from accidentally corruptnig it.

Example:

Code: Select all

from DraftGui import todo

def remove_node(self, parent, child):

    todo.delay(parent.removeChild, child)
    
todo.delay() requires one function parameter. That means it can't call functions that require two or more. To call a function which has no parameters (or doesn't require any), use

Code: Select all

todo.delay(my_fn, None)
I don't know if this will solve your problem, but by the description, it sounds like a job for todo.delay() :)
FreeCAD Trails workbench for transportation engineering: https://www.github.com/joelgraff/freecad.trails

pivy_trackers 2D coin3D library: https://www.github.com/joelgraff/pivy_trackers
Chri1
Posts: 86
Joined: Wed Oct 17, 2018 9:00 am

Re: crash by removeObject in combination wih snapper

Post by Chri1 »

Joel_graff wrote: Sat Apr 18, 2020 1:35 am

Code: Select all

todo.delay(my_fn, None)
I don't know if this will solve your problem, but by the description, it sounds like a job for todo.delay() :)
Thank you very very much, this was it!
todo.delay(sg.removeChild, sp) # sg is the sceneGraph, sp is the SoSeparator


Kind regards
Chri1
Post Reply