[gst-devel] Freeing application data from message

Tim-Philipp Müller t.i.m at zen.co.uk
Tue Jul 6 10:05:22 CEST 2010

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?

More information about the gstreamer-devel mailing list