From fde9bf4470d4a3b6ee1da0dee2370ab028b6314a Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Fri, 23 Nov 2012 09:47:14 +0100 Subject: qom: make object_delete usable for statically-allocated objects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Store in the object the freeing function that will be used at deletion time. This makes it possible to use object_delete on statically-allocated (embedded) objects. Dually, it makes it possible to use object_unparent and object_unref without leaking memory, when the lifetime of object might extend until after the call to object_delete. Reviewed-by: Andreas Färber Signed-off-by: Paolo Bonzini Signed-off-by: Anthony Liguori --- qom/object.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'qom/object.c') diff --git a/qom/object.c b/qom/object.c index 0749506..3b50255 100644 --- a/qom/object.c +++ b/qom/object.c @@ -388,6 +388,9 @@ void object_finalize(void *data) object_property_del_all(obj); g_assert(obj->ref == 0); + if (obj->free) { + obj->free(obj); + } } Object *object_new_with_type(Type type) @@ -399,6 +402,7 @@ Object *object_new_with_type(Type type) obj = g_malloc(type->instance_size); object_initialize_with_type(obj, type); + obj->free = g_free; return obj; } @@ -415,7 +419,6 @@ void object_delete(Object *obj) object_unparent(obj); g_assert(obj->ref == 1); object_unref(obj); - g_free(obj); } Object *object_dynamic_cast(Object *obj, const char *typename) -- cgit v1.1