Intro
Short: I need to (memory) sandbox a Gtk/Gstreamer application in python and restart it regularly.
Long: I am experiencing a memory leak in a library (gstreamer) that I use for a bigger chunk of code that grabs data from a camera. Just to avoid arguments about that, I tried to solve that issue since some time and could not. I need the camera data so I decided to try and hack a solution, which is supposed to be the automation of the manual hack which would be to go regularly and restart the software before the memory fills up.
However, I am not very used to Gtk at all and I have very little knowledge about pythons memory management. Therefore, I run into problems.
Setup
So now, the program is written in python using Gtk3. I have a myPythonObjectWithGtkStuff
which is essentially a more fancy version of http://bazaar.launchpad.net/~jderose/+junk/gst-examples/view/head:/webcam-1.0 . But especially the Gtk part in the __init__()
and run()
are the same. (please note this line http://bazaar.launchpad.net/~jderose/+junk/gst-examples/view/head:/webcam-1.0#L51 as well as the on_sync_message()
function, which might be a red herring, but they deal with directing a gstreamer stream to the Gtk drawing area using a xid handle)
Problem/ Solution 1
The way how I imagined it to work was to create the entire object holding all the code in a while loop. Then call at some timepoint window.destroy()
from within the Gtk mainloop. It would return to the while loop and yet another object would be created, etc.
# shitty hack to restart the Gtk application once it got destroyed
while(1):
startVC(args)
with
def startVC(args):
mainloop = GObject.MainLoop()
obj = myPythonObjectWithGtkStuff(args)
obj.run()
del obj
So far so good. Everything works if I do it as written above. The problem with this solution is that it does not free the memory of the obj
. (I think mainly the memory leak stuff remains, I am not sure).
edit:
At the moment I call window.destroy(), the Gtk window gets closed ad I can see the memory usage going down to some stable base value. Once Gtk gets started again, the memory usage returns to the previous operating memory usage plus some additional memory that corresponds to the amount the memory leak produces.
Problem/ Solution 2
Now I thought I could just start Gtk in a process and just terminate it when it returns and start a new process.
while(1):
p = Process(target=startVC, args=(args,))
p.start()
p.join()
p.terminate()
The problem here is that it starts the first time perfectly fine, but once the first process returns and the loop launches the second process I get the following error at the moment I call self.window = Gtk.Window()
in __init()__()
of myPythonObjectWithGtkStuff
:
Gdk-WARNING **: captureVideo.py: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.
which alternates a bit with
Gdk-WARNING **: captureVideo.py: Fatal IO error 0 (Success) on X server :0.
What to do?
What I need now is a solution to either of the two problems. How can I ensure that the object gets entirely deleted from memory for the first solution, or how do I avoid that X server error for solution 2.
It might be that calling self.window.destroy()
of my obj
is not enough to kill all the Gtk stuff. Is that the right way to close an Gtk application?