Original situation:
The application I\'m working on at the moment will receive notification from another application when a particular file has had
I think the problem arises from memory being partitioned into two types in Java, heap and non-heap.Your class instance foo
gets stored in heap memory while its method CheckFile
is loaded into the method area of non-heap memory. After your script finishes, there are no more references to foo
so it gets marked for garbage collection, while the Swing interface is still referring to CheckFile
, so it gets marked as in-use. I am assuming that foo.myFile
is not considered static so it also is stored in heap memory. As for the Swing interface, it's presumably still being tracked as in-use as long as the window is open and being updated by the window manager.
Edit: your solution of using a while True
loop is a correct one, in my opinion. Use it to monitor for events and when the window closes or the last line is read, exit the loop and let the program finish.
Edit 2: alternative solution - try having foo
inherit from JFrame
to make Swing keep a persistent pointer to it in its main loop as long as the window is open.
* Initial, Partial Answer (Added to Question) *
I think I just figured this out. After foo = Foo()
, with no code left to keep the module busy, it would appear that the object ceases to exist, despite the fact that the application is still running, with a Swing window doing stuff.
If I do this:
foo = Foo()
while True:
pass
Then everything works as I would expect.
I'm still confused though, as to how foo.CheckFile() was being called at all. If the problem was that foo.myFile was going out of scope and being closed, then how come foo.CheckFile() was able to be called by the JButton?
Maybe someone else can provide a better answer.