How does the GUI handle the callback references?

How does the GUI handle the callback references?

cadop5R9X9
Enthusiast Enthusiast
414 Views
2 Replies
Message 1 of 3

How does the GUI handle the callback references?

cadop5R9X9
Enthusiast
Enthusiast

I am having an issue with my GUI disconnecting from my python code.  

 

The structure is essentially a minimal entry point script with:

 

 

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        _ = config_settings(ui)

 

 
then another script that has config_settings has a try block:
 

 

        cmdDef = ui.commandDefinitions.itemById(commandId)
        if not cmdDef:
            cmdDef = ui.commandDefinitions.addButtonDefinition(commandId, commandName, commandDescription)

        handlers = []

        onCommandCreated = MyCreatedHandler(ui, handlers)
        cmdDef.commandCreated.add(onCommandCreated)
        handlers.append(onCommandCreated)

        cmdDef.execute()

        adsk.autoTerminate(False)

        return True ​

 

 
Without making this a massive code explanation, the remainder is basically a GUI that has a few dropdowns and some buttons.  The problem is, the dropdowns all work consistently and will change some setting.  However, if I press one of the buttons, it works the first time, but then won't work again (but sometimes does).  It _seems_ like the way I can get it to usually work is by adding a breakpoint in the debug mode of the calculation parts of the code.  When there is no breakpoint, it just ends up disconnecting the the gui, and nothing I press calls any of the python functions.  I.e. even if I put a breakpoint inside the class inheriting the event handler, nothing is printed and breakpoint is not hit. 

 

class MyInputChangedHandler(adsk.core.InputChangedEventHandler):
    def __init__(self, ui):
        self.ui = ui
        super().__init__()

    def notify(self, args):
        print("Entered Notify")

 

 

So the closest thing I can understand it to be is like the API is losing its reference to the GUI callbacks and the program has ended, but it was dereferenced properly so the GUI still remains.  But this is a wild guess.  

 

Has anyone experienced this or has an idea what causes it?

0 Likes
Accepted solutions (1)
415 Views
2 Replies
Replies (2)
Message 2 of 3

kandennti
Mentor
Mentor
Accepted solution

Hi @cadop5R9X9 .

 

"handlers" must be a global variable.
Handlers that lose their references will be recovered by garbage collection.

0 Likes
Message 3 of 3

cadop5R9X9
Enthusiast
Enthusiast
Well thanks, that does explain exactly the type of behavior. It seems like there should be an error thrown in this case.
0 Likes