[gst-devel] Some help needed on buffer management

Tim Müller t.i.m at zen.co.uk
Tue Dec 20 02:09:13 CET 2005


On Tue, 2005-12-20 at 00:21 +0530, Sameer Naik wrote:

> so heres what i do to push the new data to the pes adapters
> 
> guint8 *data;
> GstBuffer *buffer;
> GstAdapter *pesPacket 
> 
> (pesPacket points to the audio adapter when audio pes packet is received
> and to the video adapter when a video pes packet is received )
> 
> buffer = gst_buffer_new();
> data = (guint8 *)malloc(size);
> memcpy(auxdata, dataPtr, size);	
> gst_buffer_set_data(buffer, data, size);
> gst_adapter_push(pesPacket, buffer);

You should use gst_buffer_new_and_alloc() instead of doing the malloc
yourself. The reason this leaks is that there's GST_BUFFER_DATA and
GST_BUFFER_MALLOCDATA and only the latter is automatically freed when
the buffer is unref'ed, so you'd need to set both if you do things your
way (see the documentation for gst_buffer_set_data()).

> here dataPtr is the pointer to the data in the main GstAdapter (one
> where the data received on the source pad is pushed) and size is the
> amount of data i need to store..
> 
> i need to store the data because i have to do a gst_adapter_flush on the
> main gstadapter...
> 
> im not able to unref the buffer after pushing it into the
> adapter...doing so gives a segmentation error...

gst_adapter_push() takes ownership of the buffer you pass to it, so you
must not access that buffer after passing it to gst_adapter_push(), as
you don't know what the adapter might have done with it (it might still
be valid and alive, or it might have been freed or merged with another
buffer etc.), unless you take your own reference with gst_buffer_ref()
before pushing it into the adapter.

> the above code is repeated for every pes packet received...so new memory
> is allocated each time and not freed....resulting in i guess memory
> problems and my system does not respond...

That's probably because GST_BUFFER_MALLOCDATA has not been set, see
above. Use gst_buffer_new_and_alloc().

> i would like to know if theres any other better way to store the data in
> the adapter or somewhere else...

Even better would be if you didn't malloc and copy the data at all, but
just created sub-buffers from existing incoming buffers using
gst_buffer_create_sub(). Don't know if that is possible in your case of
course.

Cheers
 -Tim






More information about the gstreamer-devel mailing list