From 72869e235cf79a7d77f132c517279e8e8dc8002e Mon Sep 17 00:00:00 2001 From: zhanhang Date: Sun, 8 Jun 2025 16:00:32 +0800 Subject: [PATCH] Fix the memory leak bug of SystemSoundVibrator Signed-off-by: zhanhang --- a.diff | 44 +++++++++++++++++++ .../system_sound_manager_impl.cpp | 7 +-- .../system_sound_vibrator.cpp | 1 + 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 a.diff diff --git a/a.diff b/a.diff new file mode 100644 index 000000000..87edaa266 --- /dev/null +++ b/a.diff @@ -0,0 +1,44 @@ +diff --git a/frameworks/native/system_sound_manager/system_sound_manager_impl.cpp b/frameworks/native/system_sound_manager/system_sound_manager_impl.cpp +index 0cb7f55d7274e1e6beccb6c9a6fc5d6f802bc6a6..9ac507e410faca6b30a9b587e14527416694039d 100644 +--- a/frameworks/native/system_sound_manager/system_sound_manager_impl.cpp ++++ b/frameworks/native/system_sound_manager/system_sound_manager_impl.cpp +@@ -1783,7 +1783,7 @@ std::string SystemSoundManagerImpl::AddCustomizedToneByExternalUri( + MEDIA_LOGI("AddCustomizedToneByExternalUri: Start, externalUri: %{public}s", externalUri.c_str()); + std::string fdHead = "fd://"; + std::string srcPath = externalUri; +- int32_t srcFd; ++ int32_t srcFd = -1; + if (srcPath.find(fdHead) != std::string::npos) { + StrToInt(srcPath.substr(fdHead.size()), srcFd); + } else { +@@ -1794,7 +1794,9 @@ std::string SystemSoundManagerImpl::AddCustomizedToneByExternalUri( + fdHead.clear(); + return fdHead; + } +- return AddCustomizedToneByFd(context, toneAttrs, srcFd); ++ std::string result = AddCustomizedToneByFd(context, toneAttrs, srcFd); ++ close(srcFd); ++ return result; + } + + std::string SystemSoundManagerImpl::AddCustomizedToneByFd(const std::shared_ptr &context, +@@ -1982,7 +1984,6 @@ std::string SystemSoundManagerImpl::CustomizedToneWriteFile(const std::shared_pt + } + len -= bytesWritten; + } +- close(paramsForWriteFile.srcFd); + close(dstFd); + dataShareHelper->Release(); + SendCustomizedToneEvent(true, toneAttrs, paramsForWriteFile.fileSize, mimeType_, SUCCESS); +diff --git a/frameworks/native/system_sound_manager/system_sound_vibrator/system_sound_vibrator.cpp b/frameworks/native/system_sound_manager/system_sound_vibrator/system_sound_vibrator.cpp +index 2451e2e9a65d590766c933f901ad534dd241baff..2df01f8b49a839671dbed934cb593ed828034b74 100644 +--- a/frameworks/native/system_sound_manager/system_sound_vibrator/system_sound_vibrator.cpp ++++ b/frameworks/native/system_sound_manager/system_sound_vibrator/system_sound_vibrator.cpp +@@ -302,6 +302,7 @@ int32_t SystemSoundVibrator::GetVibratorDuration(const std::string &hapticUri) + int32_t result = Sensors::PreProcess(vibratorFD, vibratorPkg); + if (result != 0) { + MEDIA_LOGE("Failed to pre-process hapticUri!"); ++ Sensors::FreeVibratorPackage(vibratorPkg); + return ret; + } + int32_t delayTime = 0; diff --git a/frameworks/native/system_sound_manager/system_sound_manager_impl.cpp b/frameworks/native/system_sound_manager/system_sound_manager_impl.cpp index 0cb7f55d7..9ac507e41 100644 --- a/frameworks/native/system_sound_manager/system_sound_manager_impl.cpp +++ b/frameworks/native/system_sound_manager/system_sound_manager_impl.cpp @@ -1783,7 +1783,7 @@ std::string SystemSoundManagerImpl::AddCustomizedToneByExternalUri( MEDIA_LOGI("AddCustomizedToneByExternalUri: Start, externalUri: %{public}s", externalUri.c_str()); std::string fdHead = "fd://"; std::string srcPath = externalUri; - int32_t srcFd; + int32_t srcFd = -1; if (srcPath.find(fdHead) != std::string::npos) { StrToInt(srcPath.substr(fdHead.size()), srcFd); } else { @@ -1794,7 +1794,9 @@ std::string SystemSoundManagerImpl::AddCustomizedToneByExternalUri( fdHead.clear(); return fdHead; } - return AddCustomizedToneByFd(context, toneAttrs, srcFd); + std::string result = AddCustomizedToneByFd(context, toneAttrs, srcFd); + close(srcFd); + return result; } std::string SystemSoundManagerImpl::AddCustomizedToneByFd(const std::shared_ptr &context, @@ -1982,7 +1984,6 @@ std::string SystemSoundManagerImpl::CustomizedToneWriteFile(const std::shared_pt } len -= bytesWritten; } - close(paramsForWriteFile.srcFd); close(dstFd); dataShareHelper->Release(); SendCustomizedToneEvent(true, toneAttrs, paramsForWriteFile.fileSize, mimeType_, SUCCESS); diff --git a/frameworks/native/system_sound_manager/system_sound_vibrator/system_sound_vibrator.cpp b/frameworks/native/system_sound_manager/system_sound_vibrator/system_sound_vibrator.cpp index 2451e2e9a..2df01f8b4 100644 --- a/frameworks/native/system_sound_manager/system_sound_vibrator/system_sound_vibrator.cpp +++ b/frameworks/native/system_sound_manager/system_sound_vibrator/system_sound_vibrator.cpp @@ -302,6 +302,7 @@ int32_t SystemSoundVibrator::GetVibratorDuration(const std::string &hapticUri) int32_t result = Sensors::PreProcess(vibratorFD, vibratorPkg); if (result != 0) { MEDIA_LOGE("Failed to pre-process hapticUri!"); + Sensors::FreeVibratorPackage(vibratorPkg); return ret; } int32_t delayTime = 0; -- Gitee