解决录音Slice中录音列表获取日期导致的内存泄漏问题
authorluozheng <1732036298@qq.com>
Wed, 10 Jul 2024 03:01:32 +0000 (11:01 +0800)
committerluozheng <1732036298@qq.com>
Wed, 10 Jul 2024 03:01:32 +0000 (11:01 +0800)
code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingModel.h
code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingModel.cpp

index e482a50..4034edb 100644 (file)
@@ -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<RecordingListItem> Recordings;                 // 存放录音信息的动态数组
         std::string recordDuration;                                // 录音时长
         int index;                                                 // 保存被点击录音的下标
-        std::string yyyymmhhh_date;                                // 保存年月日时间
+        char *yyyymmhhh_date;                                      // 保存年月日时间
         std::string TotalRecordDuration;                           // 保存录音的总时长
         std::string RecordingTimeAndDuration;                      // 录音日期和录音时长
     };
index b96dfa1..f726b4e 100644 (file)
@@ -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});
             }
         }
     }