From 045b3c6e59e0373239987767b3f406f134521e0a Mon Sep 17 00:00:00 2001 From: DaShi Date: Thu, 4 Jan 2024 13:51:07 +0800 Subject: [PATCH] [Media Interface] fix mem leak and itf deletion issues fix mem leak and itf deletion issues. --- .../common/codec/hal/codechal_common.h | 6 +++ .../common/codec/hal/shared/codec_hw_next.cpp | 4 ++ .../media_interfaces_mtl.cpp | 40 ++++++++++++------- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/media_softlet/agnostic/common/codec/hal/codechal_common.h b/media_softlet/agnostic/common/codec/hal/codechal_common.h index ff52cb4965c..8fe8787912b 100644 --- a/media_softlet/agnostic/common/codec/hal/codechal_common.h +++ b/media_softlet/agnostic/common/codec/hal/codechal_common.h @@ -68,6 +68,12 @@ class CodechalSetting; #define CODECHAL_PUBLIC_CHK_NULL_NO_STATUS_RETURN(_ptr) \ MOS_CHK_NULL_NO_STATUS_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_PUBLIC, _ptr) +#define CODECHAL_PUBLIC_CHK_STATUS_WITH_DESTROY_RETURN(_stmt, destroyFunction) \ + { \ + MOS_STATUS sts = (MOS_STATUS)(_stmt); \ + MOS_CHK_COND_WITH_DESTROY_RETURN_VALUE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, (MOS_STATUS_SUCCESS != sts), destroyFunction, sts, "error status!") \ + } + typedef struct CODECHAL_SSEU_SETTING { uint8_t ui8NumSlices; diff --git a/media_softlet/agnostic/common/codec/hal/shared/codec_hw_next.cpp b/media_softlet/agnostic/common/codec/hal/shared/codec_hw_next.cpp index 04e682696ad..e5a95d59fb6 100644 --- a/media_softlet/agnostic/common/codec/hal/shared/codec_hw_next.cpp +++ b/media_softlet/agnostic/common/codec/hal/shared/codec_hw_next.cpp @@ -63,6 +63,10 @@ CodechalHwInterfaceNext::CodechalHwInterfaceNext( m_cpInterface = mhwInterfacesNext->m_cpInterface; m_veboxInterface = mhwInterfacesNext->m_veboxInterface; m_sfcInterface = mhwInterfacesNext->m_sfcInterface; + //Prevent double free + mhwInterfacesNext->m_cpInterface = nullptr; + mhwInterfacesNext->m_veboxInterface = nullptr; + mhwInterfacesNext->m_sfcInterface = nullptr; m_disableScalability = disableScalability; m_userSettingPtr = osInterface->pfnGetUserSettingInstance(osInterface); diff --git a/media_softlet/media_interface/media_interfaces_mtl/media_interfaces_mtl.cpp b/media_softlet/media_interface/media_interfaces_mtl/media_interfaces_mtl.cpp index 25e42371b37..38712359264 100644 --- a/media_softlet/media_interface/media_interfaces_mtl/media_interfaces_mtl.cpp +++ b/media_softlet/media_interface/media_interfaces_mtl/media_interfaces_mtl.cpp @@ -397,14 +397,12 @@ MOS_STATUS CodechalInterfacesXe_Lpm_Plus::Initialize( if (debugInterface == nullptr) { MOS_Delete(hwInterface); - mhwInterfaces->SetDestroyState(true); CODECHAL_PUBLIC_ASSERTMESSAGE("debugInterface is not valid!"); return MOS_STATUS_NO_SPACE; } if (debugInterface->Initialize(hwInterface, CodecFunction) != MOS_STATUS_SUCCESS) { MOS_Delete(hwInterface); - mhwInterfaces->SetDestroyState(true); MOS_Delete(debugInterface); CODECHAL_PUBLIC_ASSERTMESSAGE("Debug interface creation failed!"); return MOS_STATUS_INVALID_PARAMETER; @@ -413,6 +411,14 @@ MOS_STATUS CodechalInterfacesXe_Lpm_Plus::Initialize( CodechalDebugInterface *debugInterface = nullptr; #endif // USE_CODECHAL_DEBUG_TOOL + auto release_func = [&]() + { + MOS_Delete(hwInterface); +#if USE_CODECHAL_DEBUG_TOOL + MOS_Delete(debugInterface); +#endif // USE_CODECHAL_DEBUG_TOOL + }; + if (CodecHalIsDecode(CodecFunction)) { #ifdef _MPEG2_DECODE_SUPPORTED @@ -475,7 +481,7 @@ MOS_STATUS CodechalInterfacesXe_Lpm_Plus::Initialize( #endif { CODECHAL_PUBLIC_ASSERTMESSAGE("Decode mode requested invalid!"); - return MOS_STATUS_INVALID_PARAMETER; + CODECHAL_PUBLIC_CHK_STATUS_WITH_DESTROY_RETURN(MOS_STATUS_INVALID_PARAMETER, release_func); } if (m_codechalDevice == nullptr) @@ -486,7 +492,7 @@ MOS_STATUS CodechalInterfacesXe_Lpm_Plus::Initialize( MOS_Delete(debugInterface); #endif CODECHAL_PUBLIC_ASSERTMESSAGE("Decoder device creation failed!"); - return MOS_STATUS_NO_SPACE; + CODECHAL_PUBLIC_CHK_STATUS_WITH_DESTROY_RETURN(MOS_STATUS_NO_SPACE, release_func); } } else if (CodecHalIsEncode(CodecFunction)) @@ -500,14 +506,14 @@ MOS_STATUS CodechalInterfacesXe_Lpm_Plus::Initialize( if (m_codechalDevice == nullptr) { CODECHAL_PUBLIC_ASSERTMESSAGE("Encode state creation failed!"); - return MOS_STATUS_INVALID_PARAMETER; + CODECHAL_PUBLIC_CHK_STATUS_WITH_DESTROY_RETURN(MOS_STATUS_INVALID_PARAMETER, release_func); } return MOS_STATUS_SUCCESS; } else { CODECHAL_PUBLIC_ASSERTMESSAGE("Encode allocation failed, AVC VME Encoder is not supported, please use AVC LowPower Encoder instead!"); - return MOS_STATUS_INVALID_PARAMETER; + CODECHAL_PUBLIC_CHK_STATUS_WITH_DESTROY_RETURN(MOS_STATUS_INVALID_PARAMETER, release_func); } } else @@ -519,7 +525,7 @@ MOS_STATUS CodechalInterfacesXe_Lpm_Plus::Initialize( if (m_codechalDevice == nullptr) { CODECHAL_PUBLIC_ASSERTMESSAGE("Encode state creation failed!"); - return MOS_STATUS_INVALID_PARAMETER; + CODECHAL_PUBLIC_CHK_STATUS_WITH_DESTROY_RETURN(MOS_STATUS_INVALID_PARAMETER, release_func); } return MOS_STATUS_SUCCESS; } @@ -528,7 +534,7 @@ MOS_STATUS CodechalInterfacesXe_Lpm_Plus::Initialize( if (info->Mode == CODECHAL_ENCODE_MODE_MPEG2) { CODECHAL_PUBLIC_ASSERTMESSAGE("Encode allocation failed, MPEG2 Encoder is not supported!"); - return MOS_STATUS_INVALID_PARAMETER; + CODECHAL_PUBLIC_CHK_STATUS_WITH_DESTROY_RETURN(MOS_STATUS_INVALID_PARAMETER, release_func); } else #ifdef _JPEG_ENCODE_SUPPORTED @@ -538,7 +544,7 @@ MOS_STATUS CodechalInterfacesXe_Lpm_Plus::Initialize( if (m_codechalDevice == nullptr) { CODECHAL_PUBLIC_ASSERTMESSAGE("Encode state creation failed!"); - return MOS_STATUS_INVALID_PARAMETER; + CODECHAL_PUBLIC_CHK_STATUS_WITH_DESTROY_RETURN(MOS_STATUS_INVALID_PARAMETER, release_func); } return MOS_STATUS_SUCCESS; } @@ -550,12 +556,16 @@ MOS_STATUS CodechalInterfacesXe_Lpm_Plus::Initialize( if (CodecHalUsesVdencEngine(info->CodecFunction)) { m_codechalDevice = MOS_New(EncodeAv1VdencPipelineAdapterXe_Lpm_Plus, hwInterface, debugInterface); - CODECHAL_PUBLIC_CHK_NULL_RETURN(m_codechalDevice); + if (m_codechalDevice == nullptr) + { + CODECHAL_PUBLIC_ASSERTMESSAGE("Encode state creation failed!"); + CODECHAL_PUBLIC_CHK_STATUS_WITH_DESTROY_RETURN(MOS_STATUS_INVALID_PARAMETER, release_func); + } return MOS_STATUS_SUCCESS; } else { - return MOS_STATUS_INVALID_PARAMETER; + CODECHAL_PUBLIC_CHK_STATUS_WITH_DESTROY_RETURN(MOS_STATUS_INVALID_PARAMETER, release_func); } } else @@ -569,27 +579,27 @@ MOS_STATUS CodechalInterfacesXe_Lpm_Plus::Initialize( if (m_codechalDevice == nullptr) { CODECHAL_PUBLIC_ASSERTMESSAGE("Encode state creation failed!"); - return MOS_STATUS_INVALID_PARAMETER; + CODECHAL_PUBLIC_CHK_STATUS_WITH_DESTROY_RETURN(MOS_STATUS_INVALID_PARAMETER, release_func); } return MOS_STATUS_SUCCESS; } else { CODECHAL_PUBLIC_ASSERTMESSAGE("Encode allocation failed, HEVC VME Encoder is not supported."); - return MOS_STATUS_INVALID_PARAMETER; + CODECHAL_PUBLIC_CHK_STATUS_WITH_DESTROY_RETURN(MOS_STATUS_INVALID_PARAMETER, release_func); } } else #endif { CODECHAL_PUBLIC_ASSERTMESSAGE("Unsupported encode function requested."); - return MOS_STATUS_INVALID_PARAMETER; + CODECHAL_PUBLIC_CHK_STATUS_WITH_DESTROY_RETURN(MOS_STATUS_INVALID_PARAMETER, release_func); } } else { CODECHAL_PUBLIC_ASSERTMESSAGE("Unsupported codec function requested."); - return MOS_STATUS_INVALID_PARAMETER; + CODECHAL_PUBLIC_CHK_STATUS_WITH_DESTROY_RETURN(MOS_STATUS_INVALID_PARAMETER, release_func); } return MOS_STATUS_SUCCESS;