Debian Codegraph @

gtk/gtkmm/object.cc

raw symbols SCIP
1 /* Copyright (C) 1998-2010 The gtkmm Development Team
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include <gtkmm/object.h>
18 #include <gtkmm/private/object_p.h>
19 #include <glibmm/quark.h>
20 #include <gtk/gtk.h>
21
22 namespace Gtk
23 {
24
25 Object::Object(const Glib::ConstructParams& construct_params)
26 : Glib::Object(construct_params)
27 {
28 gobject_disposed_ = false;
29
30 _init_unmanage(); //We don't like the GTK+ default memory management - we want to be in control._)
31 }
32
33 Object::Object(GObject* castitem)
34 : Glib::Object(castitem)
35 {
36 gobject_disposed_ = false;
37
38 _init_unmanage(); //We don't like the GTK+ default memory management - we want to be in control.
39 }
40
41 void Object::_init_unmanage(bool /* is_toplevel = false */)
42 {
43 //GTKMM_LIFECYCLE
44
45 if(gobject_)
46 {
47 //Glib::Object::Object has already stored a pointer to this C++ instance in the underlying C instance,
48 //and connected a callback which will, in turn, call our destroy_notify_(),
49 //so will will know if GTK+ disposes of the underlying instance.
50
51 // Most GTK+ objects are floating, by default. This means that the container widget controls their lifetime.
52 // We'll change this:
53 if(g_object_is_floating (gobject_)) //Top-level Windows and Dialogs can not be manag()ed, so there is no need to do this.
54 {
55 GLIBMM_DEBUG_REFERENCE(this, gobject_);
56 g_object_ref_sink(gobject_); //Stops it from being floating - we will make this optional ( see Gtk::manage() ),
57
58 #ifdef GLIBMM_DEBUG_REFCOUNTING
59 g_warning("gtkmm after sink: C++ instance: %p, C instance: %p, refcount=%d\n", (void*)(Glib::ObjectBase*)this, (void*)gobject_, G_OBJECT(gobject_)->ref_count);
60 g_warning(" c instance gtype: %s\n", G_OBJECT_TYPE_NAME(gobject_));
61 #endif
62
63 referenced_ = true; //Not managed.
64 }
65 else
66 {
67 //This widget is already not floating. It's probably already been added to a GTK+ container, and has just had Glib::wrap() called on it.
68 //It's not floating because containers call g_object_sink() on child widgets to take control of them.
69 //We just ref() it so that we can unref it later.
70 //GLIBMM_DEBUG_REFERENCE(this, gobject_);
71 //g_object_ref(gobject_);
72
73 //Alternatively, it might be a top-level window (e.g. a Dialog). We would then be doing one too-many refs(),
74 //We do an extra unref() in Window::_release_c_instance() to take care of that.
75
76 referenced_ = false; //Managed. We should not try to unfloat GObjects that we did not instantiate.
77 }
78 }
79 }
80
81 void Object::_release_c_instance()
82 {
83 #ifdef GLIBMM_DEBUG_REFCOUNTING
84 g_warning("Gtk::Object::_release_c_instance() this=%p, gobject_=%p\n", (void*)(Glib::ObjectBase*)this, (void*)gobject_);
85 if(gobject_)
86 g_warning(" gtypename: %s\n", G_OBJECT_TYPE_NAME(gobject_));
87 #endif
88
89 cpp_destruction_in_progress_ = true;
90
91 // remove our hook.
92 GObject* object = gobj();
93
94 if (object)
95 {
96 g_assert(G_IS_OBJECT(object));
97
98 //We can't do anything with the gobject_ if it's already been disposed.
99 //This prevents us from unref-ing it again, or destroying it again after GTK+ has told us that it has been disposed.
100 if (!gobject_disposed_)
101 {
102 if (referenced_ || (GTK_IS_WIDGET(object) && !gtk_widget_get_parent(GTK_WIDGET(object))))
103 {
104 // It's not manage()d or it's an orphan widget, so we just unref to destroy it.
105 #ifdef GLIBMM_DEBUG_REFCOUNTING
106 g_warning("final unref: gtypename: %s, refcount: %d\n", G_OBJECT_TYPE_NAME(object), ((GObject*)object)->ref_count);
107 #endif
108
109 if (!referenced_ && g_object_is_floating(object))
110 {
111 GLIBMM_DEBUG_REFERENCE(this, object);
112 // It's floating if it's a managed widget which is not stored in a container.
113 // GTK prints a warning if a widget with a floating ref is finalized.
114 g_object_ref_sink(object); // Stop it from being floating.
115 }
116
117 GLIBMM_DEBUG_UNREFERENCE(this, object);
118 g_object_unref(object);
119
120 //destroy_notify_() should have been called after the final g_object_unref()
121 //or g_object_run_dispose(), so gobject_disposed_ could now be true.
122
123 // Note that this is not an issue for GtkWindows,
124 // because we use gtk_widget_destroy in Gtk::Window::_release_c_instance() instead.
125 //
126 //If the C instance still isn't dead then insist, by calling g_object_run_dispose().
127 //This is necessary because even a manage()d widget is refed when added to a container.
128 // <danielk> That's simply not true. But references might be taken elsewhere,
129 // and g_object_run_dispose() just tells everyone "drop your refs, please!".
130 if (!gobject_disposed_)
131 {
132 #ifdef GLIBMM_DEBUG_REFCOUNTING
133 g_warning("Gtk::Object::_release_c_instance(): Calling g_object_run_dispose(): gobject_=%p, gtypename=%s\n", (void*)object, G_OBJECT_TYPE_NAME(object));
134 #endif
135
136 g_assert(G_IS_OBJECT(object));
137 g_object_run_dispose(object); //Container widgets can respond to this.
138 }
139 }
140 else
141 {
142 //It's manag()ed, but the coder decided to delete it before deleting its parent.
143 //That should be OK because the Container can respond to that.
144 #ifdef GLIBMM_DEBUG_REFCOUNTING
145 g_warning("Gtk::Object::_release_c_instance(): Calling g_object_run_dispose(): gobject_=%p\n", (void*)gobject_);
146 #endif
147
148 if (!gobject_disposed_)
149 {
150 g_assert(G_IS_OBJECT(object));
151 g_object_run_dispose(object);
152 }
153 }
154 }
155
156 //If the GObject still exists, disconnect the C++ wrapper from it.
157 //The C++ wrapper is being deleted right now.
158 disconnect_cpp_wrapper();
159
160 //Glib::Object::~Object() will not g_object_unref() it too. because gobject_ is now 0.
161 }
162 }
163
164 Object::Object(Object&& src) noexcept
165 : Glib::Object(std::move(src)),
166 referenced_(std::move(src.referenced_)),
167 gobject_disposed_(std::move(src.gobject_disposed_))
168 {}
169
170 Object& Object::operator=(Object&& src) noexcept
171 {
172 Glib::Object::operator=(std::move(src));
173 referenced_ = std::move(src.referenced_);
174 gobject_disposed_ = std::move(src.gobject_disposed_);
175 return *this;
176 }
177
178 Object::~Object() noexcept
179 {
180 #ifdef GLIBMM_DEBUG_REFCOUNTING
181 g_warning("Gtk::Object::~Object() gobject_=%p\n", (void*)gobject_);
182 #endif
183
184 //This has probably been called already from Gtk::Object::destroy_(), which is called from derived destructors.
185 _release_c_instance();
186 }
187
188 void Object::disconnect_cpp_wrapper()
189 {
190 //GTKMM_LIFECYCLE:
191
192 #ifdef GLIBMM_DEBUG_REFCOUNTING
193 g_warning("Gtk::Object::disconnect_cpp_wrapper() this=%p, gobject_=%p\n", (void*)(Glib::ObjectBase*)this, (void*)gobject_);
194 if(gobject_)
195 g_warning(" gtypename: %s\n", G_OBJECT_TYPE_NAME(gobject_));
196 #endif
197
198 if(gobj())
199 {
200 //Prevent gtk vfuncs and default signal handlers from calling our instance methods:
201 g_object_steal_qdata((GObject*)gobj(), Glib::quark_); //It will no longer be possible to get the C++ instance from the C instance.
202
203 //Allow us to prevent generation of a new C++ wrapper during destruction:
204 g_object_set_qdata((GObject*)gobj(), Glib::quark_cpp_wrapper_deleted_, (gpointer)true);
205
206 //Prevent C++ instance from using GTK+ object:
207 gobject_ = nullptr;
208
209 //TODO: Disconnect any signals, using gtk methods.
210 //We'll have to keep a record of all the connections.
211 }
212 }
213
214 void Object::destroy_notify_()
215 {
216 //Overriden.
217 //GTKMM_LIFECYCLE
218
219 #ifdef GLIBMM_DEBUG_REFCOUNTING
220 g_warning("Gtk::Object::destroy_notify_: this=%p, gobject_=%p\n", (void*)(Glib::ObjectBase*)this, (void*)gobject_);
221 if(gobject_)
222 g_warning(" gtypename=%s\n", G_OBJECT_TYPE_NAME(gobject_));
223 #endif
224
225 //TODO: Remove gobject_disposed_ when we can break ABI.
226 // "if (gobject_disposed_)" can be replaced by "if (gobj())" or "if (gobject_)".
227 //Remember that it's been disposed (which only happens once):
228 //This also stops us from destroying it again in the destructor when it calls destroy_().
229 gobject_disposed_ = true;
230
231 //Actually this function is called when the GObject is finalized, not when it's
232 //disposed. Clear the pointer to the GObject, because otherwise it would
233 //become a dangling pointer, pointing to a non-existant object.
234 gobject_ = nullptr;
235
236 if(!cpp_destruction_in_progress_) //This function might have been called as a side-effect of destroy_() when it called g_object_run_dispose().
237 {
238 if (!referenced_) //If it's manage()ed.
239 {
240 #ifdef GLIBMM_DEBUG_REFCOUNTING
241 g_warning("Gtk::Object::destroy_notify_: before delete this.\n");
242 #endif
243 delete this; //Free the C++ instance.
244 }
245 else //It's not managed, but the C gobject_ just died before the C++ instance..
246 {
247 #ifdef GLIBMM_DEBUG_REFCOUNTING
248 g_warning("Gtk::Object::destroy_notify_: setting gobject_ to 0\n");
249 #endif
250 }
251 }
252
253 }
254
255 void Object::destroy_()
256 {
257 //Called from destructors.
258 //GTKMM_LIFECYCLE
259
260 #ifdef GLIBMM_DEBUG_REFCOUNTING
261 g_warning("Gtk::Object::destroy_(): gobject_: %p\n", (void*)gobject_);
262 if(gobject_)
263 g_warning(" gtypename: %s\n", G_OBJECT_TYPE_NAME(gobject_));
264 #endif
265
266 if ( !cpp_destruction_in_progress_ )
267 {
268 cpp_destruction_in_progress_ = true;
269
270 //destroy the C instance:
271 _release_c_instance();
272 }
273
274 //The C++ destructor will be reached later. This function was called by a destructor.
275 }
276
277 void Object::set_manage()
278 {
279 //GTKMM_LIFECYCLE
280 //This object will not be unref()ed by gtkmm, though it could be destroyed if the coder deletes the C++ instance early.
281 //This method is overriden in Gtk::Window because they can not be manage()ed.
282
283 if (!referenced_) return; //It's already managed.
284
285 // remove our reference
286 if (gobject_->ref_count >= 1) //This should only happen just after creation. We don't use "==1" because GtkButton starts with a refcount of 2 when using a mnemonic.
287 {
288 //g_warning("Object::set_manage(), making object floating: %s\n", G_OBJECT_TYPE_NAME(gobject_));
289
290 // Cowardly refuse to remove last reference make floating instead. //TODO: What does that comment mean?
291 #ifdef GLIBMM_DEBUG_REFCOUNTING
292 g_warning("Object::set_manage(): setting GTK_FLOATING: gobject_ = %p", (void*) gobj());
293 g_warning(" gtypename=%s\n", G_OBJECT_TYPE_NAME(gobj()));
294 #endif
295 //deprecated: GTK_OBJECT_SET_FLAGS(gobj(), GTK_FLOATING);
296 g_object_force_floating(gobject_);
297 }
298 else
299 {
300 g_warning("Object::set_manage(). Refcount seems to be 0. %s\n", G_OBJECT_TYPE_NAME(gobject_));
301
302 //DEF_GLIBMM_DEBUG_UNREF(this, gobj())
303 //g_object_unref(gobj());
304 }
305
306 //g_warning("Object::set_manage(): end: %s", G_OBJECT_TYPE_NAME(gobject_));
307 //g_warning(" refcount=%d", G_OBJECT(gobj())->ref_count);
308
309 referenced_ = false;
310 }
311
312 //TODO: This protected function is not used any more. Can it be removed without breaking ABI/API?
313 void Object::callback_weak_notify_(void* data, GObject* /* gobject */) //static
314 {
315 //This is only used for a short time, then disconnected.
316
317 Object* cppObject = static_cast<Object*>(data);
318 if(cppObject) //This will be 0 if the C++ destructor has already run.
319 {
320 cppObject->gobject_disposed_ = true;
321 }
322
323 //TODO: Do we need to do this?: g_object_weak_unref(gobject, &Object::callback_weak_notify_, data);
324 }
325
326 bool Object::is_managed_() const
327 {
328 return !referenced_;
329 }
330
331 } // namespace Gtk
332
333 namespace
334 {
335 } // anonymous namespace
336
337 namespace Gtk
338 {
339
340
341 /* The *_Class implementation: */
342
343 const Glib::Class& Object_Class::init()
344 {
345 if(!gtype_) // create the GType if necessary
346 {
347 // Glib::Class has to know the class init function to clone custom types.
348 class_init_func_ = &Object_Class::class_init_function;
349
350 // This is actually just optimized away, apparently with no harm.
351 // Make sure that the parent type has been created.
352 //CppClassParent::CppObjectType::get_type();
353
354 // Create the wrapper type, with the same class/instance size as the base type.
355 register_derived_type(g_object_get_type());
356
357 // Add derived versions of interfaces, if the C type implements any interfaces:
358 }
359
360 return *this;
361 }
362
363 void Object_Class::class_init_function(void* g_class, void* class_data)
364 {
365 BaseClassType *const klass = static_cast<BaseClassType*>(g_class);
366 CppClassParent::class_init_function(klass, class_data);
367 }
368
369 Glib::ObjectBase* Object_Class::wrap_new(GObject* o)
370 {
371 return manage(new Object((GObject*)(o)));
372 }
373
374 /* The implementation: */
375
376 Object::CppClassType Object::object_class_; // initialize static member
377
378 GType Object::get_type()
379 {
380 return object_class_.init().get_type();
381 }
382
383 GType Object::get_base_type()
384 {
385 return g_object_get_type();
386 }
387
388 } // namespace Gtk