memory leak while using appsink element..

Thiago Sousa Santos thiago.sousa.santos at collabora.com
Mon Dec 26 07:14:07 PST 2011


On Fri, 2011-12-23 at 07:49 -0800, juhsis wrote:
> Dear gstreamer-developers,
> 
> Here is my code for generate 2 IplImages (in OpenCv) with using gstreamer
> elements. I've created this system:
> 
> v4l2src -> videobox -> 
>                                    -> videomixer2 -> videoflip ->
> ffmpegcolorspace -> appsink 
> v4l2src -> videobox -> 
> 
> 
> When i run this code, i got my output. But memory swells. I've tried over 3
> weeks but cant fix this memory leakage. If you see the result of leakage,
> please let me know.. Thanks.
> 
> Here is my code :
> 
> 
> #include <opencv/cv.h>
> #include <opencv/highgui.h>
> #include <opencv/cxcore.h>
> #include <opencv/cvaux.h>
> #include <string.h>
> #include <stdio.h>
> #include <time.h>
> #include <sys/time.h>
> #include <gst/gst.h>
> #include <gst/app/gstappsrc.h>
> #include <gst/app/gstappsink.h>
> #include <gst/app/gstappbuffer.h>
> #define SCAL1 2
> #define SHOWIN 1
> 
> unsigned char *ptr;
> IplImage* frameL;
> IplImage* frameR;
> GstElement *pipeline;
> static void on_new_buffer_from_source (GstElement * elt, void * data)
> {
> 	guint size;
> 	GstBuffer *buffer;
> 	
> 	buffer = gst_app_sink_pull_buffer (GST_APP_SINK (elt));
> 	printf("%p\n",buffer);
> 	size = GST_BUFFER_SIZE (buffer);
> 	if(ptr==NULL)
> 		ptr = (unsigned char *)malloc(size);
> 	memcpy (ptr, GST_BUFFER_DATA (buffer), size);
> 

You should unref the buffer pulled from appsink IIRC.

> 	for(unsigned int r = 0; r < size/2; r++)
> 		frameL->imageData[r]=ptr[r];
> 	for(unsigned int r = 0; r < size/2; r++)
> 		frameR->imageData[r]=ptr[r+size/2];
> 	system("free -b");
> #if SHOWIN
>         int cmd = cvvWaitKeyEx( 0, 1 );
>         cvvShowImage( "source left", frameL );
>         cvvShowImage( "source right", frameR );
> #endif
> 
> }
> 
> int main( int argc, char** argv )
> {
> 	GstElement *ffmpegcolor = NULL;
> 	gst_init (&argc, &argv);
> 	GstElement *source,*source2,*mix;
> 	GstElement *vb,*vb2;
> 	GstElement *sink;
> 	GstElement *vf;
> 	GstPad *sink_0,*sink_1;
> 	GstPad *src_0,*src_1;
> 	GstCaps *caps,*caps2;
> 	gboolean link_ok=true;
> 	gboolean link_ok2=true;
> 	gboolean link_ok3=true;
> 
> 	caps = gst_caps_new_full (
> 		gst_structure_new ("video/x-raw-yuv",
> 			 "width", G_TYPE_INT, 640/SCAL1,
> 			 "height", G_TYPE_INT, 480/SCAL1,
> 			 "framerate", GST_TYPE_FRACTION, 30, 1,
> 			 NULL),
> 		NULL);
> 	caps2 = gst_caps_new_full (
> 		gst_structure_new ("video/x-raw-gray",
> 			 "width", G_TYPE_INT, 480/SCAL1,
> 			 "height", G_TYPE_INT, 1280/SCAL1,
> 			 "framerate", GST_TYPE_FRACTION, 30, 1,
> 			 NULL),
> 		NULL);
> 	pipeline = gst_pipeline_new ("my-pipeline");
> 	source = gst_element_factory_make("v4l2src", "source");
> 	source2 = gst_element_factory_make("v4l2src", "source2");
>  	g_object_set(G_OBJECT(source), "device", "/dev/video0", NULL);
>  	g_object_set(G_OBJECT(source2), "device", "/dev/video1", NULL);
> 	mix = gst_element_factory_make("videomixer2", "mix");
> 	vb = gst_element_factory_make("videobox", "vb");
> 	vb2 = gst_element_factory_make("videobox", "vb2");
> 	vf = gst_element_factory_make("videoflip", "vf");
>  	g_object_set(G_OBJECT(vb2), "left",(gint)-640/SCAL1, NULL);
>  	g_object_set(G_OBJECT(vf), "method",1, NULL);
> 	ffmpegcolor = gst_element_factory_make ("ffmpegcolorspace", NULL);
> 	sink = gst_element_factory_make("appsink", "sink");
> 	gst_app_sink_set_drop((GstAppSink *)sink,true);
>  	g_object_set(G_OBJECT(sink), "max-buffers",(guint)1, NULL);
>  	gst_bin_add_many(GST_BIN
> (pipeline),source,source2,vb,vb2,mix,ffmpegcolor,vf,sink, NULL);
> 	
> 	link_ok = gst_element_link_filtered (source, vb, caps);
> 	link_ok2 = gst_element_link_filtered (source2, vb2, caps);
> 
> 	sink_0 = gst_element_get_request_pad (mix, "sink_0");
> 	sink_1 = gst_element_get_request_pad (mix, "sink_1");
> 	src_0 = gst_element_get_static_pad (vb, "src");
> 	src_1 = gst_element_get_static_pad (vb2, "src");
>   	gst_pad_link (src_0,sink_1);
>   	gst_pad_link (src_1,sink_0);

Unref the pads after linking, get_*_pad functions return a new reference
that you have to handle.

> 
> 	gst_element_link_many(mix, vf, ffmpegcolor, NULL);
> 	link_ok3 = gst_element_link_filtered (ffmpegcolor,sink, caps2);
> 
> 	gst_caps_unref (caps);
> 	gst_caps_unref (caps2);
> 
> 	if (!link_ok) {
> 		g_warning ("Failed to link element1 and element2 --1--!");
> 	}
> 	if (!link_ok2) {
> 		g_warning ("Failed to link element1 and element2 --2--!");
> 	}
> 	if (!link_ok3) {
> 		g_warning ("Failed to link element1 and element2 --2--!");
> 	}
> 	g_object_set (G_OBJECT (sink), "emit-signals", TRUE, "sync", FALSE, NULL);
> 	g_signal_connect (sink, "new-buffer", G_CALLBACK
> (on_new_buffer_from_source), NULL);
> 	frameR = cvCreateImage(cvSize(480/SCAL1, 640/SCAL1), IPL_DEPTH_8U, 1);
> 	frameL = cvCreateImage(cvSize(480/SCAL1, 640/SCAL1), IPL_DEPTH_8U, 1);
> 	gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_PLAYING);
> 	g_print ("Let's run!\n");
> 	
> #if SHOWIN
>         cvNamedWindow("source left", 1);
>         cvNamedWindow("source right", 1);
> #endif
>     while(true){
> 
>     }
> 
> 
>     return 0;
> }
> 

--
Thiago

> --
> View this message in context: http://gstreamer-devel.966125.n4.nabble.com/memory-leak-while-using-appsink-element-tp4229045p4229045.html
> Sent from the GStreamer-devel mailing list archive at Nabble.com.
> _______________________________________________
> gstreamer-devel mailing list
> gstreamer-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/gstreamer-devel




More information about the gstreamer-devel mailing list