[gst-devel] How to handle the memory allocation after gst_caps_new_simple? How to use gst_caps_unref(caps) about this cases?

Donny Viszneki donny.viszneki at gmail.com
Mon Feb 16 04:06:21 CET 2009


On Sun, Feb 15, 2009 at 8:14 PM, xiupeng xie <xiexiupeng at gmail.com> wrote:
> 1.approach:
>
> GstCaps *caps;
>
> Caps = gst_caps_new_simple(....);  // for example, let 0x12345678 be the
> address of memory.
>
> g_object_set(G_OBJECT(caps_filter), "caps", Caps // it will be 0x12345678.
>
> gst_caps_unref(caps); // the purpose of caps has finished, since the all
> detailed parameter  under property "caps" of "caps_filter" has been
> assigned.
>
> For case1, distinctly we have to call gst_caps_unref() to release?

This is how I believe it is done in the documentation, but assuming
that the GstCaps structure never gets modified indirectly in any way,
I cannot think of a reason you can't keep "reusing" it throughout your
application.

> In case 2, How can your codes to notify g_object_set() to call
> g_object_unref() so that to release this memory?

gstreamer code (or any other glib-related code) is responsible for
respective gobject reference counting semantics. When you "give" the
GstCaps (or any gobject instance) to any other piece of code, it is
responsible for incrementing the reference counter so that it will
remain allocated, and then decreasing its reference counter whenever
it is finished using it, so that it may be freed.

If the GstCaps (or any similar gobject in a similar usage scenario)
will remain present throughout your application, you can either leave
it (it will be deallocated when your process exits) or you can
g_object_unref() whenever it is logical to do so (whenever you no
longer need it) and you generally should not worry about whether or
not another piece of code (ie. gstreamer) needs to hold on to a
reference to that gobject instance or not.

The only rule is that you should never decrement the reference count
of a gobject instance before you are finished with it, and always do
so at the moment you are finished with it. If "someone else" is not
finished with it, they will keep a reference to it on their own, and
you don't have to concern yourself with it.

-- 
http://codebad.com/




More information about the gstreamer-devel mailing list