On 13/07/2018 10:27, Thomas Huth wrote: > A lot of code is using the object_initialize() function followed by a call > to object_property_add_child() to add the newly initialized object as a child > of the current object. Both functions increase the reference counter of the > new object, but many spots that call these two functions then forget to drop > one of the superfluous references. So the newly created object is often not > cleaned up correctly when the parent is destroyed. In the worst case, this > can cause crashes, e.g. because device objects are not correctly removed from > their parent_bus. > > Since this is a common pattern between many code spots, let's introdcue a > new function that takes care of calling all three required initialization > functions, first object_initialize(), then object_property_add_child() and > finally object_unref(). > > And while we're at object.h, also fix some copy-n-paste errors in the > comments there ("to store the area" --> "to store the error").
Even though I'd prefer the full cleanup, I can live with this. :) Series Reviewed-by: Paolo Bonzini <pbonz...@redhat.com> except for patch 6 for which I've sent a replacement. Paolo > Signed-off-by: Thomas Huth <th...@redhat.com> > --- > include/qom/object.h | 23 +++++++++++++++++++++-- > qom/object.c | 15 +++++++++++++++ > 2 files changed, 36 insertions(+), 2 deletions(-) > > diff --git a/include/qom/object.h b/include/qom/object.h > index f3d2308..3362db0 100644 > --- a/include/qom/object.h > +++ b/include/qom/object.h > @@ -749,6 +749,25 @@ int object_set_propv(Object *obj, > void object_initialize(void *obj, size_t size, const char *typename); > > /** > + * object_initialize_child: > + * @parentobj: The parent object to add a property to > + * @propname: The name of the property > + * @childobj: A pointer to the memory to be used for the object. > + * @size: The maximum size available at @obj for the object. > + * @type: The name of the type of the object to instantiate. > + * @errp: If an error occurs, a pointer to an area to store the error > + * > + * This function will initialize an object. The memory for the object should > + * have already been allocated. The object will then be added as child > property > + * to a parent with object_property_add_child() function. The returned object > + * has a reference count of 1 (for the "child<...>" property from the > parent), > + * so the object will get finalized automatically when the parent gets > removed. > + */ > +void object_initialize_child(Object *parentobj, const char *propname, > + void *childobj, size_t size, const char *type, > + Error **errp); > + > +/** > * object_dynamic_cast: > * @obj: The object to cast. > * @typename: The @typename to cast to. > @@ -1382,7 +1401,7 @@ Object *object_resolve_path_component(Object *parent, > const gchar *part); > * @obj: the object to add a property to > * @name: the name of the property > * @child: the child object > - * @errp: if an error occurs, a pointer to an area to store the area > + * @errp: if an error occurs, a pointer to an area to store the error > * > * Child properties form the composition tree. All objects need to be a > child > * of another object. Objects can only be a child of one object. > @@ -1420,7 +1439,7 @@ void object_property_allow_set_link(const Object *, > const char *, > * @child: a pointer to where the link object reference is stored > * @check: callback to veto setting or NULL if the property is read-only > * @flags: additional options for the link > - * @errp: if an error occurs, a pointer to an area to store the area > + * @errp: if an error occurs, a pointer to an area to store the error > * > * Links establish relationships between objects. Links are unidirectional > * although two links can be combined to form a bidirectional relationship > diff --git a/qom/object.c b/qom/object.c > index 4609e34..7be7638 100644 > --- a/qom/object.c > +++ b/qom/object.c > @@ -392,6 +392,21 @@ void object_initialize(void *data, size_t size, const > char *typename) > object_initialize_with_type(data, size, type); > } > > +void object_initialize_child(Object *parentobj, const char *propname, > + void *childobj, size_t size, const char *type, > + Error **errp) > +{ > + object_initialize(childobj, size, type); > + object_property_add_child(parentobj, propname, OBJECT(childobj), errp); > + /* > + * Since object_property_add_child added a reference to the child object, > + * we can drop the reference added by object_initialize(), so the child > + * property will own the only reference to the object. > + */ > + object_unref(OBJECT(childobj)); > +} > + > + > static inline bool object_property_is_child(ObjectProperty *prop) > { > return strstart(prop->type, "child<", NULL); >