blob: 5d17185bce634eba6acb8b064599c0c19f7b9603 [file] [log] [blame]
Ownership of dynamic objects
Any object-oriented system or language that doesn't have automatic garbage
collection has many potential pitfalls as far as the pointers go. Therefore,
some standards must be adhered to as far as who owns what.
Arguments passed into a function are owned by the caller, and the function
will make a copy of the string for its own internal use. The string should
be const gchar *. Strings returned from a function are always a copy of the
original and should be freed after usage by the caller.
name = gst_element_get_name (element); /* copy of name is made */
.. use name ..
g_free (name); /* free after usage */
Objects passed into a function are owned by the caller, any additional
reference held to the object after leaving the function should increase the
refcount of that object.
Objects returned from a function are owned by the caller. This means that the
called should _free() or _unref() the object after usage.
peer = gst_pad_get_peer (pad); /* peer with increased refcount */
if (peer) {
.. use peer ..
gst_object_unref (GST_OBJECT (peer)); /* unref peer after usage */
When retrieving multiple objects from an object an iterator should be used.
The iterator allows you to access the objects one after another while making
sure that the set of objects retrieved remains consistent.
Each object retrieved from an iterator has its refcount increased or is a
copy of the original. In any case the object should be unreffed or freed
after usage.