using cv::VideoWriter with nvv4l2h264enc (DPS4.0)and memory leak

Hi,
I using this sample to test cv::VideoWriter.And Memory leak problem happens.How can I release these memory?Is there any other way to save Mat into video file?

void writeMP4File()
{
	string videoPath = "./test.mp4";
	int frameRate = 6;
	Mat bmp = imread ("./test_1.png");
	cv::Size frame_size(bmps[0].cols,bmps[0].rows);
	std::string  gst_writer = "appsrc ! video/x-raw, format=(string)BGR ! \
                              videoconvert ! video/x-raw, format=(string)I420 ! \
                              nvvideoconvert ! video/x-raw(memory:NVMM) ! \
                              nvv4l2h264enc ! h264parse ! matroskamux ! filesink location=" + videoPath;
	cv::VideoWriter channelVideoWriter = cv::VideoWriter(gst_writer, cv::VideoWriter::fourcc('a','v','c','1'), frameRate, frame_size, true);
	int frame = 0;
	while(frame < 40)
	{
		channelVideoWriter.write(bmp);
		frame++;
	}	
	channelVideoWriter.release();
}

Hi quanminzhu,

This seems an issue on openCV , please refer to this thread from openCV forum if more suggestions:
https://answers.opencv.org/questions/scope:all/sort:activity-desc/page:1/query:VideoWriter/

Hi,there.Is there any way to do this without opencv?

Hi,there.
Actually,It is not about openCV.
If I use VideoWriter without nvv4l2h264enc from deepstream-4.0SDK,Memory leak problem never happens.

string videoPath = "./test.mp4";
      cv::VideoWriter channelVideoWriter = cv::VideoWriter(videoPath, cv::VideoWriter::fourcc('a','v','c','1'), frameRate, frame_size, true);

And If I use these VideoWriter,Memory leak obviously.Runing function of writeMP4File 100 times with 2GB memory leak.

std::string  gst_writer = "appsrc ! video/x-raw, format=(string)BGR ! \
                              videoconvert ! video/x-raw, format=(string)I420 ! \
                              nvvideoconvert ! video/x-raw(memory:NVMM) ! \
                              nvv4l2h264enc ! h264parse ! matroskamux ! filesink location=" + videoPath;
	cv::VideoWriter channelVideoWriter = cv::VideoWriter(gst_writer, cv::VideoWriter::fourcc('a','v','c','1'), frameRate, frame_size, true);

Could you test this and help me?

Hi,
We don’t observe any memory leak in running

$ gst-launch-1.0 videotestsrc ! video/x-raw,width=1920,height=1080,format=BGR ! videoconvert ! video/x-raw,format=I420 ! nvvideoconvert ! 'video/x-raw(memory:NVMM)' ! nvv4l2h264enc ! h264parse ! matroskamux ! fakesink

nvvideoconvert and nvv4l2h264enc are widely used in DeepStream SDK, it is unilkely having memory leak.

Are you able you run pure gstreamer pipeline in your case? you may leverage pngdec plugins. Here is a reference post.