Your problem is pretty subtle. This usually happens due to the rules of ownership and destruction of GObject / GtkObject. Let me remind them:
GObject simply refers to a number. They are destroyed when the counter reaches 0. The newly created object has a count of 1.GInitiallyUnowned also refers to a number, and they are also destroyed when the counter reaches 0. But the newly created object has a floating point number. This means that the first time the counter should increase, it does not actually increase, but the floating counter drops, that is, it is converted to a normal link.
GtkObject are GInitiallyUnowned objects, so they have a magic floating point number.
But you probably know all this ... Now, my question is:
Who owns the visible main GtkWindow counter?
It's really simple, the GTK framework has a list of them and maintains a link to all visible GtkWindow . But then another question arises:
When does a GTK frame free links to its GtkWindow?
Do you remember the gtk_widget_destroy() function and the destroy signal? They are designed precisely for this: when you want to remove toplevel GtkWindow , you call gtk_widget_destroy() , it activates the destroy signal, which receives a GTK frame, which removes the actual window and releases the link to the object.
And here is the cause of your problem: if the GTK structure only retains the existing link to GtkWindow , the object is actually freed. If then your timer tries to access it, it will not work, because the window is no longer there.
And finally, the solution comes (hopefully):
- Call
g_object_ref()/g_object_ref_sink() in the window when the timer starts. Also register a handler for the destroy window signal. - In the window
destroy signal handler: call g_object_unref() on the window and stop the timer.
Naturally, this partial solution should also work, because the window will not be destroyed without sending the destroy signal:
- Register a window
destroy signal handler. - In the window
destroy signal handler: stop the timer.
But it is considered good practice to escalate ref-counter objects when you actually keep a pointer to them.
source share