Freeing application data from message

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Freeing application data from message

Albert Costa
Hi,
I have a plugin that generates custom application messages to be sent on the bus:

///////////
//INSIDE MY PLUGIN

  GstMessage* msg;
  GstStructure* structure;
  GstBus* bus = gst_element_get_bus(GST_ELEMENT(filter));

  MyInfo* myinfo = new MyInfo;
  /* do stuff with myinfo, including allocating internal data */

  structure = gst_structure_new("FILTERMESSAGE",
    "info", G_TYPE_POINTER, (gpointer)myinfo, 
    NULL); 
  msg = gst_message_new_application(NULL,structure);
  gst_bus_post (bus, msg);


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

All this works fine, but how can I free the resources allocated in myinfo structure? I can't release the strcuture that is owned by message, and I try to unref the message in the application then I get a 
 CRITICAL **: file ..\..\..\Source\gstreamer\gst\gstminiobject.c: line 359: assertion `mini_object->refcount > 0' failed

So how can I release my data?
Regards,
Al



------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gstreamer-devel
Reply | Threaded
Open this post in threaded view
|

Re: Freeing application data from message

Robert Powell-2


On Mon, Jul 5, 2010 at 5:11 AM, Albert Costa <[hidden email]> 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); 
  msg = gst_message_new_application(NULL,structure);
  gst_bus_post (bus, msg);



According to the docs, gst_message_new_application will take ownership of the structure, so you should release it after you call gst_message_new_application.

Hope that helps.

Rob

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gstreamer-devel
Reply | Threaded
Open this post in threaded view
|

Re: Freeing application data from message

Tim-Philipp Müller-2
On Mon, 2010-07-05 at 09:10 -0700, Robert Powell wrote:
>
> According to the docs, gst_message_new_application will take ownership
> of the structure, so you should release it after you call
> gst_message_new_application.

Quite the opposite, gst_message_new_application() takes ownership of the
structure, so you *don't* have to release it afterwards, the
GstStructure will be freed when the message gets freed.

 Cheers
  -Tim



------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gstreamer-devel
Reply | Threaded
Open this post in threaded view
|

Re: Freeing application data from message

Tim-Philipp Müller-2
In reply to this post by Albert Costa
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
this:

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:
>     case GST_MESSAGE_APPLICATION:
>         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).
>
Cheers
 -Tim

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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gstreamer-devel
Reply | Threaded
Open this post in threaded view
|

Re : Freeing application data from message

Albert Costa
Hi Tim,
thanks for the help, I'll give it a try and see how that works.
Concerning the other problem, I've submitted the bug with a short extract of my file that still generates the problem: Bug 623663
Regards,
Al


De : Tim-Philipp Müller <[hidden email]>
À : [hidden email]
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
this:

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:
>    case GST_MESSAGE_APPLICATION:
>        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).
>
Cheers
-Tim

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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gstreamer-devel


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gstreamer-devel
Reply | Threaded
Open this post in threaded view
|

Re : Freeing application data from message

Albert Costa
In reply to this post by Tim-Philipp Müller-2
Hi Tim,
tried and approved, thanks a lot again for your help!
Regards,
Al


De : Tim-Philipp Müller <[hidden email]>
À : [hidden email]
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
this:

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:
>    case GST_MESSAGE_APPLICATION:
>        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).
>
Cheers
-Tim

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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gstreamer-devel


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gstreamer-devel