Non-Modal WPF window instance still in memory?

Hi,
I have a small tool I made in a non-modal window, with the help of the example in this link:
https://pythoncvc.net/?p=247
I’ve noticed that when using this specific tool, Revit becomes slower and slower, and hogs up more memory until it is almost unusable. Restarting Revit solves it.
This does not happen with any other script I have.
Also the same script when used in a regular bundle button, behaves nicely.
My assumption, that is that the instance of the non-modal window stays in memory, has anyone else encountered this? ideas?

I have to see the code but if you close the window after it being used, it should get cleaned up by the garbage collector

Hi,
Thanks for the reply, I guess I missed it

from Autodesk.Revit.UI import IExternalEventHandler, ExternalEvent
from Autodesk.Revit.DB import Transaction
from Autodesk.Revit.Exceptions import InvalidOperationException
import rpw
from pyrevit.forms import WPFWindow
doc = rpw.revit.doc
uidoc = rpw.revit.uidoc
from piping import run, test
from piper import Piper
import traceback



def pick_main_pipe():
    piper.set_main_pipe_and_point()

# Create a subclass of IExternalEventHandler
class SimpleEventHandler(IExternalEventHandler):
    def __init__(self, do_this):
        self.do_this = do_this


    def Execute(self, uiapp):
        try:
            self.do_this()
        except Exception:
            print('<>'*60)
            print(traceback.format_exc())
            print('<>'*60)

        except InvalidOperationException:
            # If you don't catch this exeption Revit may crash.
            print "InvalidOperationException catched"
                    

    def GetName(self):
        return "simple function executed by an IExternalEventHandler in a Form"

piper = Piper(uidoc)
simple_event_handler = SimpleEventHandler(pick_main_pipe)
# We now need to create the ExternalEvent
ext_event = ExternalEvent.Create(simple_event_handler)

class ModelessForm(WPFWindow):

    def __init__(self, xaml_file_name):
        WPFWindow.__init__(self, xaml_file_name)
        self.text1.Text = "Set main pipe plane. Select any pipe on that plane:"
        self.plane_text.Text = str(piper.plane)
        self.Show()

    def pick_main(self, sender, e):
        # This Raise() method launch a signal to Revit to tell him you want to do something in the API context
        ext_event.Raise()
        piper.deg = float(self.textbox_deg.Text)
        piper.diameter = float(self.textbox_diameter.Text)
        self.plane_text.Text = str(piper.plane)


modeless_form = ModelessForm("./pyrevit_button/pyrevit_button.xaml")
1 Like