2016-12-05 21:12:24 +00:00
|
|
|
|
# Ownership of dynamic objects
|
|
|
|
|
|
|
|
|
|
Any object-oriented system or language that doesn’t have automatic
|
2016-12-30 07:20:50 +00:00
|
|
|
|
garbage collection has many potential pitfalls as far as pointers
|
2016-12-05 21:12:24 +00:00
|
|
|
|
go. Therefore, some standards must be adhered to as far as who owns
|
|
|
|
|
what.
|
|
|
|
|
|
|
|
|
|
## Strings
|
|
|
|
|
|
|
|
|
|
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
|
2016-12-30 07:20:50 +00:00
|
|
|
|
string should be `const gchar *`. Strings returned from a function are
|
2016-12-05 21:12:24 +00:00
|
|
|
|
always a copy of the original and should be freed after usage by the
|
|
|
|
|
caller.
|
|
|
|
|
|
|
|
|
|
ex:
|
|
|
|
|
|
|
|
|
|
``` c
|
2017-01-23 21:49:36 +00:00
|
|
|
|
name = gst_element_get_name (element); /* copy of name is made */
|
|
|
|
|
.. use name ..
|
|
|
|
|
g_free (name); /* free after usage */
|
2016-12-05 21:12:24 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Objects
|
|
|
|
|
|
|
|
|
|
Objects passed into a function are owned by the caller, any additional
|
|
|
|
|
reference held to the object after leaving the function should increase
|
2016-12-30 07:20:50 +00:00
|
|
|
|
its refcount.
|
2016-12-05 21:12:24 +00:00
|
|
|
|
|
|
|
|
|
Objects returned from a function are owned by the caller. This means
|
2016-12-30 07:20:50 +00:00
|
|
|
|
that the caller should `_free()` or `_unref()` the objects after usage.
|
2016-12-05 21:12:24 +00:00
|
|
|
|
|
|
|
|
|
ex:
|
|
|
|
|
|
|
|
|
|
``` c
|
2017-01-23 21:49:36 +00:00
|
|
|
|
peer = gst_pad_get_peer (pad); /* peer with increased refcount */
|
|
|
|
|
if (peer) {
|
|
|
|
|
.. use peer ..
|
|
|
|
|
gst_object_unref (GST_OBJECT (peer)); /* unref peer after usage */
|
|
|
|
|
}
|
2016-12-05 21:12:24 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Iterators
|
|
|
|
|
|
2016-12-30 07:20:50 +00:00
|
|
|
|
When retrieving multiple objects from an object, an iterator should be
|
2016-12-05 21:12:24 +00:00
|
|
|
|
used. The iterator allows you to access the objects one after another
|
2016-12-30 07:20:50 +00:00
|
|
|
|
while making sure that the retrieved set of objects remains consistent.
|
2016-12-05 21:12:24 +00:00
|
|
|
|
|
|
|
|
|
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.
|