Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH v2 1/3] avutil/hwcontext_qsv: register free function for device_derive
@ 2023-06-19  6:15 Tong Wu
  2023-06-19  6:15 ` [FFmpeg-devel] [PATCH v2 2/3] lavu/hwcontext_qsv: fix memory leak for d3d11va impl Tong Wu
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Tong Wu @ 2023-06-19  6:15 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Tong Wu, Wenbin Chen

When qsv device is created by device_derive, the ctx->free function is
not registered, causing potential memory leak because of not properly
closing the MFX session.

Signed-off-by: Tong Wu <tong1.wu@intel.com>
Signed-off-by: Wenbin Chen <wenbin.chen@intel.com>
---
 libavutil/hwcontext_qsv.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/libavutil/hwcontext_qsv.c b/libavutil/hwcontext_qsv.c
index 4ed4242ddf..878ecd886e 100644
--- a/libavutil/hwcontext_qsv.c
+++ b/libavutil/hwcontext_qsv.c
@@ -2094,6 +2094,15 @@ static int qsv_device_derive(AVHWDeviceContext *ctx,
                              AVDictionary *opts, int flags)
 {
     mfxIMPL impl;
+    QSVDevicePriv *priv;
+
+    priv = av_mallocz(sizeof(*priv));
+    if (!priv)
+        return AVERROR(ENOMEM);
+
+    ctx->user_opaque = priv;
+    ctx->free = qsv_device_free;
+    
     impl = choose_implementation("hw_any", child_device_ctx->type);
     return qsv_device_derive_from_child(ctx, impl,
                                         child_device_ctx, flags);
-- 
2.41.0.windows.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [FFmpeg-devel] [PATCH v2 2/3] lavu/hwcontext_qsv: fix memory leak for d3d11va impl
  2023-06-19  6:15 [FFmpeg-devel] [PATCH v2 1/3] avutil/hwcontext_qsv: register free function for device_derive Tong Wu
@ 2023-06-19  6:15 ` Tong Wu
  2023-06-19  6:15 ` [FFmpeg-devel] [PATCH v2 3/3] lavu/hwcontext_qsv: fix memory leak for d3d9 impl Tong Wu
  2023-06-20  2:34 ` [FFmpeg-devel] [PATCH v2 1/3] avutil/hwcontext_qsv: register free function for device_derive Xiang, Haihao
  2 siblings, 0 replies; 4+ messages in thread
From: Tong Wu @ 2023-06-19  6:15 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Tong Wu

Signed-off-by: Tong Wu <tong1.wu@intel.com>
---
 libavutil/hwcontext_qsv.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/libavutil/hwcontext_qsv.c b/libavutil/hwcontext_qsv.c
index 878ecd886e..1d24a04561 100644
--- a/libavutil/hwcontext_qsv.c
+++ b/libavutil/hwcontext_qsv.c
@@ -665,6 +665,7 @@ static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
 
 static int qsv_d3d11_update_config(void *ctx, mfxHDL handle, mfxConfig cfg)
 {
+    int ret = AVERROR_UNKNOWN;
 #if CONFIG_D3D11VA
     mfxStatus sts;
     IDXGIAdapter *pDXGIAdapter;
@@ -679,7 +680,8 @@ static int qsv_d3d11_update_config(void *ctx, mfxHDL handle, mfxConfig cfg)
         hr = IDXGIDevice_GetAdapter(pDXGIDevice, &pDXGIAdapter);
         if (FAILED(hr)) {
             av_log(ctx, AV_LOG_ERROR, "Error IDXGIDevice_GetAdapter %d\n", hr);
-            goto fail;
+            IDXGIDevice_Release(pDXGIDevice);
+            return ret;
         }
 
         hr = IDXGIAdapter_GetDesc(pDXGIAdapter, &adapterDesc);
@@ -689,7 +691,7 @@ static int qsv_d3d11_update_config(void *ctx, mfxHDL handle, mfxConfig cfg)
         }
     } else {
         av_log(ctx, AV_LOG_ERROR, "Error ID3D11Device_QueryInterface %d\n", hr);
-        goto fail;
+        return ret;
     }
 
     impl_value.Type = MFX_VARIANT_TYPE_U16;
@@ -722,11 +724,13 @@ static int qsv_d3d11_update_config(void *ctx, mfxHDL handle, mfxConfig cfg)
         goto fail;
     }
 
-    return 0;
+    ret = 0;
 
 fail:
+    IDXGIAdapter_Release(pDXGIAdapter);
+    IDXGIDevice_Release(pDXGIDevice);
 #endif
-    return AVERROR_UNKNOWN;
+    return ret;
 }
 
 static int qsv_d3d9_update_config(void *ctx, mfxHDL handle, mfxConfig cfg)
-- 
2.41.0.windows.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [FFmpeg-devel] [PATCH v2 3/3] lavu/hwcontext_qsv: fix memory leak for d3d9 impl
  2023-06-19  6:15 [FFmpeg-devel] [PATCH v2 1/3] avutil/hwcontext_qsv: register free function for device_derive Tong Wu
  2023-06-19  6:15 ` [FFmpeg-devel] [PATCH v2 2/3] lavu/hwcontext_qsv: fix memory leak for d3d11va impl Tong Wu
@ 2023-06-19  6:15 ` Tong Wu
  2023-06-20  2:34 ` [FFmpeg-devel] [PATCH v2 1/3] avutil/hwcontext_qsv: register free function for device_derive Xiang, Haihao
  2 siblings, 0 replies; 4+ messages in thread
From: Tong Wu @ 2023-06-19  6:15 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Tong Wu

Signed-off-by: Tong Wu <tong1.wu@intel.com>
---
 libavutil/hwcontext_qsv.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/libavutil/hwcontext_qsv.c b/libavutil/hwcontext_qsv.c
index 1d24a04561..82c020915d 100644
--- a/libavutil/hwcontext_qsv.c
+++ b/libavutil/hwcontext_qsv.c
@@ -756,25 +756,28 @@ static int qsv_d3d9_update_config(void *ctx, mfxHDL handle, mfxConfig cfg)
     hr = IDirect3DDeviceManager9_LockDevice(devmgr, device_handle, &device, TRUE);
     if (FAILED(hr)) {
         av_log(ctx, AV_LOG_ERROR, "Error LockDevice %d\n", hr);
+        IDirect3DDeviceManager9_CloseDeviceHandle(devmgr, device_handle);
         goto fail;
     }
 
     hr = IDirect3DDevice9Ex_GetCreationParameters(device, &params);
     if (FAILED(hr)) {
         av_log(ctx, AV_LOG_ERROR, "Error IDirect3DDevice9_GetCreationParameters %d\n", hr);
+        IDirect3DDevice9Ex_Release(device);
         goto unlock;
     }
 
     hr = IDirect3DDevice9Ex_GetDirect3D(device, &d3d9ex);
     if (FAILED(hr)) {
         av_log(ctx, AV_LOG_ERROR, "Error IDirect3DDevice9Ex_GetAdapterLUID %d\n", hr);
+        IDirect3DDevice9Ex_Release(device);
         goto unlock;
     }
 
     hr = IDirect3D9Ex_GetAdapterLUID(d3d9ex, params.AdapterOrdinal, &luid);
     if (FAILED(hr)) {
         av_log(ctx, AV_LOG_ERROR, "Error IDirect3DDevice9Ex_GetAdapterLUID %d\n", hr);
-        goto unlock;
+        goto release;
     }
 
     impl_value.Type = MFX_VARIANT_TYPE_PTR;
@@ -784,13 +787,18 @@ static int qsv_d3d9_update_config(void *ctx, mfxHDL handle, mfxConfig cfg)
     if (sts != MFX_ERR_NONE) {
         av_log(ctx, AV_LOG_ERROR, "Error adding a MFX configuration"
                "DeviceLUID property: %d.\n", sts);
-        goto unlock;
+        goto release;
     }
 
     ret = 0;
 
+release:
+    IDirect3D9Ex_Release(d3d9ex);
+    IDirect3DDevice9Ex_Release(device);
+
 unlock:
     IDirect3DDeviceManager9_UnlockDevice(devmgr, device_handle, FALSE);
+    IDirect3DDeviceManager9_CloseDeviceHandle(devmgr, device_handle);
 fail:
 #endif
     return ret;
-- 
2.41.0.windows.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [FFmpeg-devel] [PATCH v2 1/3] avutil/hwcontext_qsv: register free function for device_derive
  2023-06-19  6:15 [FFmpeg-devel] [PATCH v2 1/3] avutil/hwcontext_qsv: register free function for device_derive Tong Wu
  2023-06-19  6:15 ` [FFmpeg-devel] [PATCH v2 2/3] lavu/hwcontext_qsv: fix memory leak for d3d11va impl Tong Wu
  2023-06-19  6:15 ` [FFmpeg-devel] [PATCH v2 3/3] lavu/hwcontext_qsv: fix memory leak for d3d9 impl Tong Wu
@ 2023-06-20  2:34 ` Xiang, Haihao
  2 siblings, 0 replies; 4+ messages in thread
From: Xiang, Haihao @ 2023-06-20  2:34 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Wu, Tong1, Chen, Wenbin

On Ma, 2023-06-19 at 14:15 +0800, Tong Wu wrote:
> When qsv device is created by device_derive, the ctx->free function is
> not registered, causing potential memory leak because of not properly
> closing the MFX session.
> 
> Signed-off-by: Tong Wu <tong1.wu@intel.com>
> Signed-off-by: Wenbin Chen <wenbin.chen@intel.com>
> ---
>  libavutil/hwcontext_qsv.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/libavutil/hwcontext_qsv.c b/libavutil/hwcontext_qsv.c
> index 4ed4242ddf..878ecd886e 100644
> --- a/libavutil/hwcontext_qsv.c
> +++ b/libavutil/hwcontext_qsv.c
> @@ -2094,6 +2094,15 @@ static int qsv_device_derive(AVHWDeviceContext *ctx,
>                               AVDictionary *opts, int flags)
>  {
>      mfxIMPL impl;
> +    QSVDevicePriv *priv;
> +
> +    priv = av_mallocz(sizeof(*priv));
> +    if (!priv)
> +        return AVERROR(ENOMEM);
> +
> +    ctx->user_opaque = priv;
> +    ctx->free = qsv_device_free;
> +    
>      impl = choose_implementation("hw_any", child_device_ctx->type);
>      return qsv_device_derive_from_child(ctx, impl,
>                                          child_device_ctx, flags);

Patchset LGTM and will apply

Thanks
Haihao


_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-06-20  2:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-19  6:15 [FFmpeg-devel] [PATCH v2 1/3] avutil/hwcontext_qsv: register free function for device_derive Tong Wu
2023-06-19  6:15 ` [FFmpeg-devel] [PATCH v2 2/3] lavu/hwcontext_qsv: fix memory leak for d3d11va impl Tong Wu
2023-06-19  6:15 ` [FFmpeg-devel] [PATCH v2 3/3] lavu/hwcontext_qsv: fix memory leak for d3d9 impl Tong Wu
2023-06-20  2:34 ` [FFmpeg-devel] [PATCH v2 1/3] avutil/hwcontext_qsv: register free function for device_derive Xiang, Haihao

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git