Python and GTK+: How to create garbage collector friendly objects?

泪湿孤枕 提交于 2019-12-11 07:44:36

问题


I started writing a small application in Python with GTK+ as my widget toolkit. Recently I decided to do some memory optimization and noticed that a most of PyGTK objects I create never gets released by garbage collector. This number keeps growing even if I open a window and properly destroy it afterward.

Can someone point me in the right direction on how to create and handle GTK+ objects from Python. Am not using Glade or anything like that.

Also I noticed that creating window like this:

class SomeWindow:
   def __init__(self):
      self.window = gtk.Window(type=gtk.WINDOW_TOPLEVEL)

Instead of:

class SomeWindow(gtk.Window):
   def __init__(self):
      super(SomeWindow, self).__init__(type=gtk.WINDOW_TOPLEVEL)

Gives 2+ objects less in GC list.

Any advices you can give me?


回答1:


Did you call .destroy()?

According to the gtk.Widget.destroy docu GTK maintains a list of toplevel windows that will keep a reference alive.

And in case you use any Python destructors, they can also prevent cyclic references from being freed.



来源:https://stackoverflow.com/questions/6866578/python-and-gtk-how-to-create-garbage-collector-friendly-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!