Restarting the pipeline all the time will cause memory leaks in the gstreamer
Hello everyone,
I need to restart the pipeline constantly. I find that the memory will rise (memory leak?), I have tried many versions of gstreamer, including Gstreamer1.14.5, 1.18.2, 1.20.2,1.20.3, 1.20.4 , The following is a demo
test.c need to change the location of rtspsrc
#include <locale.h>
#include <glib.h>
#include <gst/gst.h>
#include <glib-unix.h>
#include <stdio.h>
#include <stdbool.h>
static gboolean
bus_init_call(GstBus *bus,
GstMessage *msg,
gpointer data)
{
switch (GST_MESSAGE_TYPE(msg))
{
case GST_MESSAGE_EOS:
g_print("EOS\n");
return TRUE;
case GST_MESSAGE_ERROR:
{
gchar *debug;
GError *error;
gst_message_parse_error(msg, &error, &debug);
g_free(debug);
g_printerr("\n [ bus init ] from element : %s \n error message : %s \n",GST_OBJECT_NAME(msg->src),error->message);
g_error_free(error);
return TRUE;
}
default:
break;
}
return TRUE;
}
static gboolean
quit_loop (gpointer data)
{
g_print("quit \n");
GMainLoop *loop = (GMainLoop*)data;
g_main_loop_quit(loop);
return false;
}
int main(int argc, char *argv[]){
/* Initialize GStreamer */
gst_init (&argc, &argv);
for (int i=0;i<2;i++){
GMainLoop *mainloop = g_main_loop_new(NULL, FALSE);
gint bus_id;
GstElement* pipeline =
gst_parse_launch
("rtspsrc location=rtsp://admin:passwd@ip ! parsebin ! h264parse ! avdec_h264 ! fakesink ",
NULL);
g_print("playing\n");
GstBus * bus = gst_element_get_bus (pipeline);
bus_id = gst_bus_add_watch(bus, bus_init_call,NULL);
gst_object_unref (bus);
GstStateChangeReturn a = gst_element_set_state (pipeline, GST_STATE_PLAYING);
guint c = g_timeout_add_seconds(30,(GSourceFunc)quit_loop,mainloop);
g_print("run\n");
g_main_loop_run(mainloop);
GstStateChangeReturn b = gst_element_set_state(pipeline,GST_STATE_NULL);
g_print("unref \n");
gst_object_unref(pipeline);
g_main_loop_unref(mainloop);
}
gst_deinit();
return 0;
}
Makefile
CC := gcc
LIBS := $(shell pkg-config --libs --cflags gstreamer-1.0 )
CFLAGS := -g
test: test.c
"$(CC)" $(CFLAGS) $^ $(LIBS) -o $@
There are two problems in the above code.
- After the pipe is closed( code : "gst_element_set_state(pipeline,GST_STATE_NULL)"), the memory is not reduced.
- Constantly re creating and closing the pipeline will cause the memory to increase all the time.According to different element, the number of Memory add from < (lt) 10M to > (gt) 100M over a period of time ( 0.5 h ~ 3 day).
This problem can be reproduced using any element and any version of Gstreamer,This problem occurs even when using Rust.(Python and java have GC, which has no reference significance.)This problem is 100% recurrent
Same question link:
This problem will make it impossible for me to restart a pipeline in the program for many times, and I can only restart the program every time.
Is my method of use wrong? Look forward to your reply, thank you.