From 0d04660c5dca53096db1711d7925e1920685b137 Mon Sep 17 00:00:00 2001 From: Mengkejiergeli Ba Date: Mon, 4 Dec 2023 16:34:42 +0800 Subject: [PATCH] msdk: Fix memory leaks Part-of: --- .../sys/msdk/gstmsdkallocator_libva.c | 35 ++++++++++++++----- .../gst-plugins-bad/sys/msdk/gstmsdkcaps.c | 4 +++ .../gst-plugins-bad/sys/msdk/gstmsdkcontext.c | 3 +- .../gst-plugins-bad/sys/msdk/gstmsdkdec.c | 16 ++++++--- .../gst-plugins-bad/sys/msdk/gstmsdkenc.c | 2 ++ .../gst-plugins-bad/sys/msdk/gstmsdkvpp.c | 3 ++ 6 files changed, 50 insertions(+), 13 deletions(-) diff --git a/subprojects/gst-plugins-bad/sys/msdk/gstmsdkallocator_libva.c b/subprojects/gst-plugins-bad/sys/msdk/gstmsdkallocator_libva.c index 0ad508cd45..395097147c 100644 --- a/subprojects/gst-plugins-bad/sys/msdk/gstmsdkallocator_libva.c +++ b/subprojects/gst-plugins-bad/sys/msdk/gstmsdkallocator_libva.c @@ -42,6 +42,13 @@ #define GST_MSDK_FRAME_SURFACE gst_msdk_frame_surface_quark_get () +static void +gst_msdk_surface_list_free (GstMsdkSurface * surface) +{ + gst_buffer_unref (surface->buf); + g_slice_free (GstMsdkSurface, surface); +} + mfxStatus gst_msdk_frame_alloc (mfxHDL pthis, mfxFrameAllocRequest * req, mfxFrameAllocResponse * resp) @@ -55,8 +62,6 @@ gst_msdk_frame_alloc (mfxHDL pthis, mfxFrameAllocRequest * req, mfxU32 fourcc = req->Info.FourCC; mfxU16 surfaces_num = req->NumFrameSuggested; GList *tmp_list = NULL; - GList *l; - GstMsdkSurface *tmp_surface = NULL; VAStatus va_status; /* MFX_MAKEFOURCC('V','P','8','S') is used for MFX_FOURCC_VP9_SEGMAP surface @@ -210,17 +215,15 @@ gst_msdk_frame_alloc (mfxHDL pthis, mfxFrameAllocRequest * req, gst_msdk_context_add_alloc_response (context, msdk_resp); - /* We need to put all the buffers back to the pool */ - for (l = tmp_list; l; l = l->next) { - tmp_surface = (GstMsdkSurface *) l->data; - gst_buffer_unref (tmp_surface->buf); - } + g_list_free_full (tmp_list, (GDestroyNotify) gst_msdk_surface_list_free); return status; error_alloc: g_slice_free1 (surfaces_num * sizeof (mfxMemId), mids); g_slice_free (GstMsdkAllocResponse, msdk_resp); + if (tmp_list) + g_list_free_full (tmp_list, (GDestroyNotify) gst_msdk_surface_list_free); return MFX_ERR_MEMORY_ALLOC; } @@ -622,6 +625,19 @@ error_create_surface: } } +static void +gst_msdk_qdata_free (gpointer data) +{ + mfxFrameSurface1 *mfx_surface = data; + + if (mfx_surface->Data.Locked == 0) { + GstMsdkMemoryID *msdk_mid = (GstMsdkMemoryID *) mfx_surface->Data.MemId; + + g_slice_free (GstMsdkMemoryID, msdk_mid); + g_slice_free (mfxFrameSurface1, mfx_surface); + } +} + /* Currently parameter map_flag is not useful on Linux */ GstMsdkSurface * gst_msdk_import_to_msdk_surface (GstBuffer * buf, GstMsdkContext * msdk_context, @@ -633,6 +649,7 @@ gst_msdk_import_to_msdk_surface (GstBuffer * buf, GstMsdkContext * msdk_context, GstMsdkSurface *msdk_surface = NULL; mfxFrameSurface1 *mfx_surface = NULL; GstMsdkMemoryID *msdk_mid = NULL; + GDestroyNotify qdata_destroy = NULL; mem = gst_buffer_peek_memory (buf, 0); msdk_surface = g_slice_new0 (GstMsdkSurface); @@ -662,9 +679,10 @@ gst_msdk_import_to_msdk_surface (GstBuffer * buf, GstMsdkContext * msdk_context, gst_msdk_set_mfx_frame_info_from_video_info (&frame_info, vinfo); mfx_surface->Info = frame_info; + qdata_destroy = gst_msdk_qdata_free; /* Set mfxFrameSurface1 as qdata in buffer */ gst_mini_object_set_qdata (GST_MINI_OBJECT_CAST (mem), - GST_MSDK_FRAME_SURFACE, mfx_surface, NULL); + GST_MSDK_FRAME_SURFACE, mfx_surface, qdata_destroy); msdk_surface->surface = mfx_surface; @@ -795,5 +813,6 @@ gst_msdk_get_supported_modifiers (GstMsdkContext * context, gst_video_format_to_string (format), DRM_FORMAT_MOD_INVALID); } + gst_object_unref (display); g_value_unset (&gmod); } diff --git a/subprojects/gst-plugins-bad/sys/msdk/gstmsdkcaps.c b/subprojects/gst-plugins-bad/sys/msdk/gstmsdkcaps.c index 65efc8d2f9..7a2a705249 100644 --- a/subprojects/gst-plugins-bad/sys/msdk/gstmsdkcaps.c +++ b/subprojects/gst-plugins-bad/sys/msdk/gstmsdkcaps.c @@ -876,6 +876,8 @@ _enc_get_supported_formats_and_profiles (mfxSession * session, g_strfreev (profs); } + g_value_unset (&fmts); + if (gst_value_list_get_size (supported_fmts) == 0 || gst_value_list_get_size (supported_profs) == 0) return FALSE; @@ -2018,6 +2020,8 @@ gst_msdkcaps_set_strings (GstCaps * caps, } } + gst_caps_features_free (f); + if (!s) return FALSE; diff --git a/subprojects/gst-plugins-bad/sys/msdk/gstmsdkcontext.c b/subprojects/gst-plugins-bad/sys/msdk/gstmsdkcontext.c index f9faa57b48..a48be65105 100644 --- a/subprojects/gst-plugins-bad/sys/msdk/gstmsdkcontext.c +++ b/subprojects/gst-plugins-bad/sys/msdk/gstmsdkcontext.c @@ -178,11 +178,12 @@ gst_msdk_context_use_vaapi (GstMsdkContext * context) } display_drm = gst_va_display_drm_new_from_path (path); + g_free (path); + if (!display_drm) { GST_ERROR ("Couldn't create a VA DRM display"); return FALSE; } - g_free (path); priv->display = display_drm; diff --git a/subprojects/gst-plugins-bad/sys/msdk/gstmsdkdec.c b/subprojects/gst-plugins-bad/sys/msdk/gstmsdkdec.c index 4a3e2170a8..2c8c0b6194 100644 --- a/subprojects/gst-plugins-bad/sys/msdk/gstmsdkdec.c +++ b/subprojects/gst-plugins-bad/sys/msdk/gstmsdkdec.c @@ -536,6 +536,7 @@ pad_accept_memory (GstMsdkDec * thiz, const gchar * mem_type, GstCaps ** filter) gst_video_dma_drm_fourcc_to_string (fourcc, thiz->modifier); gst_msdkcaps_set_strings (caps, mem_type, "drm-format", drm_str); gst_caps_set_simple (caps, "format", G_TYPE_STRING, "DMA_DRM", NULL); + g_free (drm_str); } } #endif @@ -598,6 +599,7 @@ gst_msdkdec_fixate_format (GstMsdkDec * thiz, GstCaps * caps, gchar *drm_str = gst_video_dma_drm_fourcc_to_string (fourcc, thiz->modifier); g_value_set_string (&gfmt, drm_str); + g_free (drm_str); if (!gst_value_can_intersect (&gfmt, drm_fmts)) goto failed; gst_structure_set_value (s, "drm-format", &gfmt); @@ -667,6 +669,7 @@ gst_msdkdec_set_src_caps (GstMsdkDec * thiz, gboolean need_allocation) (GstVaDisplay *) gst_msdk_context_get_va_display (thiz->context); thiz->modifier = gst_va_dmabuf_get_modifier_for_format (display, format, VA_SURFACE_ATTRIB_USAGE_HINT_DECODER); + gst_object_unref (display); #endif #if (MFX_VERSION >= 1022) @@ -694,6 +697,9 @@ gst_msdkdec_set_src_caps (GstMsdkDec * thiz, gboolean need_allocation) gst_caps_set_value (temp_caps, "width", &v_width); gst_caps_set_value (temp_caps, "height", &v_height); + g_value_unset (&v_width); + g_value_unset (&v_height); + if (gst_caps_is_empty (gst_pad_peer_query_caps (GST_VIDEO_DECODER (thiz)->srcpad, temp_caps))) { if (!gst_util_fraction_multiply (width, height, @@ -1173,6 +1179,8 @@ gst_msdkdec_stop (GstVideoDecoder * decoder) gst_object_unref (thiz->other_pool); thiz->other_pool = NULL; } + gst_object_replace ((GstObject **) & thiz->alloc_pool, NULL); + gst_video_info_init (&thiz->non_msdk_pool_info); gst_msdkdec_close_decoder (thiz, TRUE); @@ -1757,6 +1765,8 @@ gst_msdk_create_va_pool (GstMsdkDec * thiz, GstVideoInfo * info, allocator = gst_va_allocator_new (display, formats); } + gst_object_unref (display); + if (!allocator) { GST_ERROR_OBJECT (thiz, "Failed to create allocator"); if (formats) @@ -1852,6 +1862,8 @@ gst_msdkdec_create_buffer_pool (GstMsdkDec * thiz, GstVideoInfo * info, GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT); gst_buffer_pool_config_set_video_alignment (config, &align); + gst_caps_unref (caps); + if (!gst_buffer_pool_set_config (pool, config)) goto error_pool_config; @@ -2075,25 +2087,21 @@ gst_msdkdec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query) gst_query_set_nth_allocation_pool (query, 0, pool, size, min_buffers, max_buffers); - gst_caps_unref (pool_caps); gst_object_unref (pool); return TRUE; failed_to_parse_caps: GST_ERROR_OBJECT (decoder, "failed to set buffer pool config"); - gst_caps_unref (pool_caps); return FALSE; failed_to_create_pool: GST_ERROR_OBJECT (decoder, "failed to set buffer pool config"); - gst_caps_unref (pool_caps); gst_object_unref (pool); return FALSE; error_set_config: GST_ERROR_OBJECT (decoder, "failed to set buffer pool config"); - gst_caps_unref (pool_caps); gst_object_unref (pool); return FALSE; } diff --git a/subprojects/gst-plugins-bad/sys/msdk/gstmsdkenc.c b/subprojects/gst-plugins-bad/sys/msdk/gstmsdkenc.c index a6b5a2395f..1f4be287c0 100644 --- a/subprojects/gst-plugins-bad/sys/msdk/gstmsdkenc.c +++ b/subprojects/gst-plugins-bad/sys/msdk/gstmsdkenc.c @@ -1186,6 +1186,8 @@ gst_msdk_create_va_pool (GstMsdkEnc * thiz, GstVideoInfo * info, allocator = gst_va_allocator_new (display, formats); } + gst_object_unref (display); + if (!allocator) { GST_ERROR_OBJECT (thiz, "failed to create allocator"); if (formats) diff --git a/subprojects/gst-plugins-bad/sys/msdk/gstmsdkvpp.c b/subprojects/gst-plugins-bad/sys/msdk/gstmsdkvpp.c index 9e74d2d530..22182d4ff7 100644 --- a/subprojects/gst-plugins-bad/sys/msdk/gstmsdkvpp.c +++ b/subprojects/gst-plugins-bad/sys/msdk/gstmsdkvpp.c @@ -413,6 +413,9 @@ gst_msdk_create_va_pool (GstMsdkVPP * thiz, GstVideoInfo * info, } allocator = gst_va_allocator_new (display, formats); } + + gst_object_unref (display); + if (!allocator) { GST_ERROR ("Failed to create allocator"); if (formats)