I’m fed up with this ‘segmentation fault’. It shows up whenever I try to add a TreeView to Notebook or add a button to Notebook. I just can’t understand if it arises due to Gtk or any other part of my code. Assuming it is completely because of GUI, why does it appear and how can I avoid it? Is it possible for me to track what caused it?
It would be hard to say without a code snippet, but the usual way to track these down is using
gdb or some other debugger. I usually use it via gnome-builder so I can quickly jump to files/lines.
If this is new to you, segmentation faults often happen when a caller frees memory it doesn’t own, or before it has been transferred. Maybe you’re freeing widgets (floating reference) after passing them to GtkNotebook?
No I didn’t do that. How to debug GUI applications? Will normal debuggers work?
As @andyholmes said, you can use GDB to get a stack trace of the segfault, which should tell you what you’re doing wrong—typically passing something that is not a valid object as an argument to some other function, or calling a method on a reference to an object instance that was already finalised.
Just because you’re using Python it doesn’t mean you won’t have problems; GTK is implemented in C, and it expects valid data. If the underlying data wrapped by a Python object goes away, or is invalid, you will get a segmentation fault.
Thanks a lot. I also have this doubt, what is proper way to delete a widget from memory. Shall I use
If you want to explicitly destroy a widget, use
widget.destroy(), but unless you’re trying to replace a widget from its parent, or unless you’re explicitly destroying a top level window, you should not really do that at all.
Plus, you should never use the underlying C API for memory management.
I have a widget, that is no longer used (is an orphan data), should I destroy it using
widget.destroy or Python’s garbage collector will automatically remove that widget from memory?
If you are not using a widget any more, remove all reference you have in your Python code, and the GC will collect the underlying C instance as well. Releasing a reference in Python means:
self._button = Gtk.Button() ... del self._button # or self._button = None
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.