Skip to content
This repository was archived by the owner on Mar 4, 2024. It is now read-only.

Commit f316f40

Browse files
authored
Merge pull request #214 from YaLTeR/gobject-dispose
glib: Hook up dispose() in ObjectImpl
2 parents b10a29d + 7eea6e7 commit f316f40

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

glib/src/subclass/object.rs

+23
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ pub trait ObjectImpl: ObjectSubclass + ObjectImplExt {
4141
fn constructed(&self, obj: &Self::Type) {
4242
self.parent_constructed(obj);
4343
}
44+
45+
/// Disposes of the object.
46+
///
47+
/// When `dispose()` ends, the object should not hold any reference to any other member object.
48+
/// The object is also expected to be able to answer client method invocations (with possibly an
49+
/// error code but no memory violation) until it is dropped. `dispose()` can be executed more
50+
/// than once.
51+
fn dispose(&self, _obj: &Self::Type) {}
4452
}
4553

4654
unsafe extern "C" fn get_property<T: ObjectImpl>(
@@ -92,6 +100,20 @@ unsafe extern "C" fn constructed<T: ObjectImpl>(obj: *mut gobject_ffi::GObject)
92100
imp.constructed(&from_glib_borrow::<_, Object>(obj).unsafe_cast_ref());
93101
}
94102

103+
unsafe extern "C" fn dispose<T: ObjectImpl>(obj: *mut gobject_ffi::GObject) {
104+
let instance = &*(obj as *mut T::Instance);
105+
let imp = instance.get_impl();
106+
107+
imp.dispose(&from_glib_borrow::<_, Object>(obj).unsafe_cast_ref());
108+
109+
// Chain up to the parent's dispose.
110+
let data = T::type_data();
111+
let parent_class = data.as_ref().get_parent_class() as *mut gobject_ffi::GObjectClass;
112+
if let Some(ref func) = (*parent_class).dispose {
113+
func(obj);
114+
}
115+
}
116+
95117
/// Definition of a property.
96118
#[derive(Clone)]
97119
pub struct Property<'a>(pub &'a str, pub fn(&str) -> crate::ParamSpec);
@@ -273,6 +295,7 @@ unsafe impl<T: ObjectImpl> IsSubclassable<T> for Object {
273295
klass.set_property = Some(set_property::<T>);
274296
klass.get_property = Some(get_property::<T>);
275297
klass.constructed = Some(constructed::<T>);
298+
klass.dispose = Some(dispose::<T>);
276299
}
277300
}
278301

0 commit comments

Comments
 (0)