1

i am working on a (pretty big) Python/Tkinter-application (Python 2.7 under Windows 7) which (among many other things) calls Matlab via the COM-interface. The basic structure of the Matlab/COM-part is like this:

import Tkinter
import pythoncom
import win32com.client

class App( object ):

    def __init__( self, parent ):
        Tkinter.Button( root, text="Start Matlab", command=self.start_matlab ).grid()

    def start_matlab( self ):
        self.matlab = win32com.client.Dispatch( "Matlab.Application" )

root = Tkinter.Tk()
App( root )
root.mainloop()

The behaviour i observe with this simplified code is: Running the application and clicking the button creates a Matlab-Instance (opens a Matlab-Command window), and when closing the Tkinter application also that Matlab-window and the corresponding entry in the Task-Manager disappear. Repeating the procedure, Matlab is started afresh. However, when i do "the same" with my "real" application, the Matlab instance persists after closing my application and, moreover, when i restart the application and run the part which "starts" Matlab, it just retrieves and uses the instance which remained in memory after quitting the first sessiong of my app. Unfortunately, i am not able to isolate a reasonably small code example showing the latter behavior:(

Does anybody have an idea what the reason for this is/could be?

How can one control whether a COM-object is killed or persists in memory when the parent Python application which created it is closed?

  • I can't test this, but could you try to use http://effbot.org/tkinterbook/wm.htm#Tkinter.Wm.protocol-method to capture the close window event and `del self.matlab` before the program closes? – atlasologist Apr 07 '14 at 16:55
  • @atlasologist: Thanks:) This works fine and answers one part of my question (though I am still wondering: I tried to call "del self.matlab" in the destructor of the App object, but this destructor seems not to be called when the program is closed via the x-Button...). But the other way round, is there a way to explicitly control that the Matlab instance remains persistent in memory even when the whole Python/Tkinter-application is closed? – Pascal31459 Apr 08 '14 at 08:22
  • I don't think there's a way to make the COM object persist in memory--I think it just will. Check my answer for a code sample that incorporates the protocol for destroying the window and deleting the object. – atlasologist Apr 08 '14 at 16:34

1 Answers1

1

Here's how to explicitly remove the COM object, using Tkinter's protocol handler:

import Tkinter
import pythoncom
import win32com.client

class App( object ):

    def __init__( self, parent ):
        self.parent = parent #reference to root
        Tkinter.Button( root, text="Start Matlab", command=self.start_matlab ).grid()
        self.parent.protocol('WM_DELETE_WINDOW', self.closeAll) #protocol method

    def start_matlab( self ):
        self.matlab = win32com.client.Dispatch( "Matlab.Application" )

    def closeAll(self):
        del self.matlab       #delete the COM object
        self.parent.destroy() #close the window

root = Tkinter.Tk()
App( root )
root.mainloop()

Reference: Removing COM object from memory

More on protocols

atlasologist
  • 3,824
  • 1
  • 21
  • 35