[gst-devel] Re : Freeing application data from message

Albert Costa costa_albert at yahoo.fr
Tue Jul 6 11:34:43 CEST 2010

Hi Tim,
tried and approved, thanks a lot again for your help!

De : Tim-Philipp Müller <t.i.m at zen.co.uk>
À : gstreamer-devel at lists.sourceforge.net
Envoyé le : Mar 6 juillet 2010, 10h 05min 22s
Objet : Re: [gst-devel] Freeing application data from message

On Mon, 2010-07-05 at 12:11 +0000, Albert Costa wrote:

>   MyInfo* myinfo = new MyInfo;
>   /* do stuff with myinfo, including allocating internal data */
>   structure = gst_structure_new("FILTERMESSAGE",
>     "info", G_TYPE_POINTER, (gpointer)myinfo, 
>     NULL); 

The problem here is that you're using G_TYPE_POINTER. If you use
G_TYPE_POINTER, the type system doesn't know how to copy or free your
data, so it's up to you to take care of this.

What you should do is register a 'boxed type' for your structure, like

static MyInfo *
my_info_ref (MyInfo * info)
  if (info != NULL)
    g_atomic_int_inc (&info->refcount);

  return info;

static void
my_info_unref (MyInfo * info)
  /* if we ended up with the refcount at zero, free it */
  if (g_atomic_int_dec_and_test (&info->refcount)) {
     /* g_free (info->foo); */
     /* g_free (info);      */

#define MY_TYPE_INFO my_info_get_type()

static GType
my_info_get_type (void)
  static volatile gsize type = 0;
  if (g_once_init_enter (&type)) {
    GType tmp;
    tmp = g_boxed_type_register_static ("MyInfo",
        (GBoxedCopyFunc) my_info_ref,
        (GBoxedFreeFunc) my_info_unref);
    g_once_init_leave (&type, tmp);
  return type;

Then you can use:

> structure = gst_structure_new("FILTERMESSAGE",
>     "info", MY_TYPE_INFO, myinfo, NULL);
> my_info_unref (myinfo);

and the info will be freed automatically with the message later (unless
you are still holding refs).

>   gst_bus_post (bus, msg);

Use gst_element_post_message().

> On the other side, I have my application that watches the bus
> messages, and gets my info:
>         const GstStructure *structure =
> gst_message_get_structure(message);
>         MyInfo* info;
>         info =
> (MyInfo*)g_value_get_pointer(gst_structure_get_value(structure,
> "info"));

You can then use _get_boxed() here (to just get a pointer to your info
structure), or _dup_boxed() (to get a pointer and a reference).

PS: you still have not filed a bug about that MTS typefind problem from
a few days ago. Could you please do that and/or send me the (beginning
of the) file?

This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
gstreamer-devel mailing list
gstreamer-devel at lists.sourceforge.net

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/gstreamer-devel/attachments/20100706/5c9a6498/attachment.htm>

More information about the gstreamer-devel mailing list