From: luozheng <1732036298@qq.com> Date: Wed, 10 Jul 2024 03:01:32 +0000 (+0800) Subject: 解决录音Slice中录音列表获取日期导致的内存泄漏问题 X-Git-Tag: release_V1.2.70_20241020_135402~834 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=c683e0c39b5dbaf1208f1e07a6af640ebd2f05f4;p=mirrors%2FFD230.git 解决录音Slice中录音列表获取日期导致的内存泄漏问题 --- diff --git a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingModel.h b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingModel.h index e482a5022..4034edb3a 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingModel.h +++ b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingModel.h @@ -81,7 +81,8 @@ namespace OHOS void Recording_StartTimer(uint32_t interval); void Recording_StopTimer(void); char *DisplayRecordDuration(void); - const char * GetRecordDirectory() const; + const char *GetRecordDirectory() const; + private: RecordingModel(); RecordingModel(const RecordingModel &) = delete; @@ -110,7 +111,7 @@ namespace OHOS std::vector Recordings; // 存放录音信息的动态数组 std::string recordDuration; // 录音时长 int index; // 保存被点击录音的下标 - std::string yyyymmhhh_date; // 保存年月日时间 + char *yyyymmhhh_date; // 保存年月日时间 std::string TotalRecordDuration; // 保存录音的总时长 std::string RecordingTimeAndDuration; // 录音日期和录音时长 }; diff --git a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingModel.cpp index b96dfa169..f726b4e6c 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingModel.cpp @@ -501,7 +501,20 @@ namespace OHOS void RecordingModel::InitRecordingListItem(void) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordingModel::InitRecordingListItem"); - yyyymmhhh_date = uapi_get_local_yyyymmdd_date(); // 获取日期 + // 获取日期 + char *yyyymmhhh_date_new = uapi_get_local_yyyymmdd_date(); + if (yyyymmhhh_date_new == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to get local date"); + return; // 或者进行适当的错误处理 + } + // 释放之前分配的日期字符串内存(如果之前有) + if (yyyymmhhh_date != nullptr) + { + free(yyyymmhhh_date); + } + // 更新日期字符串成员 + yyyymmhhh_date = yyyymmhhh_date_new; RecordingMp3FileCount = 0; if (!fileNames.empty()) { @@ -522,7 +535,7 @@ namespace OHOS // 对 Recordings 数组进行操作 for (int i = 0; i < RecordingMp3FileCount; i++) { - Recordings.push_back({i, fileNames[i].c_str(), "09:51", yyyymmhhh_date.c_str()}); + Recordings.push_back({i, fileNames[i].c_str(), "09:51", yyyymmhhh_date}); } } }