订正部分内存泄漏以及规范 110/4 110/head
authorCaisking <3449750381@qq.com>
Mon, 29 Apr 2024 09:17:34 +0000 (17:17 +0800)
committerCaisking <3449750381@qq.com>
Mon, 29 Apr 2024 09:17:34 +0000 (17:17 +0800)
code/sdk/application/wearable/fd230app/nativeui/bloodoxygen/src/BloodOxygenPresenter.cpp
code/sdk/application/wearable/fd230app/nativeui/heartrate/src/HeartPresenter.cpp
code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelTime.cpp
code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp
code/sdk/application/wearable/fd230app/nativeui/traing/training_list_fitness/include/TraingFitnessModel.h
code/sdk/application/wearable/fd230app/nativeui/traing/training_list_fitness/src/TraingFitnessModel.cpp
code/sdk/application/wearable/nativeapp/nativeui/phoneservice/src/PhoneService.cpp

index 6884a28..c290b75 100644 (file)
@@ -28,6 +28,8 @@
 #include "graphic_service.h"
 #include <chrono>
 #include <thread>
+#include <sys/time.h>
+
 #include "ppg_service.h"
 #include "displaybrightness/include/DisplayBrightnessModel.h"
 #include "power_display_service.h"
@@ -45,12 +47,20 @@ namespace OHOS
     // 获取当前的"时间分"
     uint64_t BloodOxygenPresenter::GetCurrentTime()
     {
-        time_t now = time(0);
-        struct tm *tblock = localtime(&now);
 
-        uint64_t current_time = tblock->tm_hour * 60 + tblock->tm_min;
+        // time_t now = time(0);
+        // struct tm *tblock = localtime(&now);
+
+        /*优化为更安全的localtime_r()*/
+        struct timeval tv;
+        gettimeofday(&tv, NULL);
+        struct tm tblock;
+        localtime_r(&tv.tv_sec, &tblock);
+
+        uint64_t current_time = tblock.tm_hour * 60 + tblock.tm_min;
         return current_time;
     }
+
     // 获取时间差,现在传进来的是两个时间戳
     std::string BloodOxygenPresenter::GetTDOA(uint64_t new_time, uint64_t old_time)
     {
index 50dce11..929a961 100644 (file)
@@ -40,10 +40,16 @@ namespace OHOS
 
     uint64_t HeartPresenter::GetCurrentTime()
     {
-        time_t now = time(0);
-        struct tm *tblock = localtime(&now);
+        // time_t now = time(0);
+        // struct tm *tblock = localtime(&now);
 
-        uint64_t current_time = tblock->tm_hour * 60 + tblock->tm_min;
+        /*优化为更安全的localtime_r()*/
+        struct timeval tv;
+        gettimeofday(&tv, NULL);
+        struct tm tblock;
+        localtime_r(&tv.tv_sec, &tblock);
+
+        uint64_t current_time = tblock.tm_hour * 60 + tblock.tm_min;
         return current_time;
     }
 
index dbc4eff..d95831e 100644 (file)
@@ -70,10 +70,18 @@ static float ConvertHandSecondValueToAngle(int32_t handValue, uint16_t range)
 
 void DialModelTime::UpdateTime()
 {
+#if 0
     struct timeval tv;
     gettimeofday(&tv, NULL);
     tblock_ = localtime(&tv.tv_sec);
     int realMS = tblock_->tm_sec * S_MS_US_TRANS + tv.tv_usec / S_MS_US_TRANS;
+#endif
+    /*优化为更安全的localtime_r()*/
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+    struct tm tblock;
+    localtime_r(&tv.tv_sec, &tblock);
+    int realMS = tblock.tm_sec * S_MS_US_TRANS + tv.tv_usec / S_MS_US_TRANS;
 
     angleH_ = ConvertHandValueToAngle(tblock_->tm_hour, HALF_DAY_IN_HOUR, tblock_->tm_min, ONE_HOUR_IN_MINUTE);
     angleM_ = ConvertHandValueToAngle(tblock_->tm_min, ONE_HOUR_IN_MINUTE, tblock_->tm_sec, ONE_MINUTE_IN_SECOND);
index f918a57..f5fee7e 100644 (file)
@@ -873,74 +873,58 @@ namespace OHOS
             FILE *fp = fopen(file_path, "ab");
             if (fp == NULL)
             {
-                fopen(file_path, "wb+");
+                // 未存储分配函数funcName的返回值
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for append");
+                fp = fopen(file_path, "wb+");
+                if (fp == NULL)
+                {
+                    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for write");
+                    return -1; // Return an error code to indicate file open failure
+                }
             }
-#if HARD_OFF
-            int ret = 0;
 
-            ret = fwrite((void *)&data, sizeof(Trainingdata), 1, fp);
+#if HARD_OFF
+            int ret = fwrite((void *)&data, sizeof(Trainingdata), 1, fp);
             if (ret != 1)
             {
-                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_data is error! %d", ret);
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_data error! %d", ret);
             }
 #else
-        if (fp == NULL)
-        {
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "file is NULL");
-        }
         Trainingdata_hard hard = {0};
         fseek(fp, 0, SEEK_SET);
         int ret = fread((void *)&hard, sizeof(Trainingdata_hard), 1, fp);
-
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "hard num %d", hard.num);
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fread %d", ret);
         if (ret == 0)
         {
+            // Write new header if it doesn't exist
             hard.num = 1;
             hard.st = timeday;
             fseek(fp, 0, SEEK_SET);
             ret = fwrite((void *)&hard, sizeof(Trainingdata_hard), 1, fp);
-
-            if (ret != 1)
-            {
-                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_hard %d", ret);
-            }
-
-            ret = fwrite((void *)&data, sizeof(Trainingdata), 1, fp);
             if (ret != 1)
             {
-                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "1 fwrite_data %d", ret);
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_hard error! %d", ret);
             }
         }
         else
         {
-            ret = fseek(fp, 8, SEEK_END);
-            if (ret != 0)
-            {
-                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fseek %d", ret);
-            }
-            hard.num++;
-            ret = fwrite((void *)&(hard), sizeof(Trainingdata_hard), 1, fp);
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "hard num %d", hard.num);
-            if (ret)
-            {
-                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_hard %d", ret);
-            }
-
             fseek(fp, 0, SEEK_END);
-            // fclose(fp);
-
-            // fp= fopen(file_path,"ab+");
-            ret = fwrite((void *)&data, sizeof(Trainingdata), 1, fp);
+            hard.num++;
+            ret = fwrite((void *)&hard, sizeof(Trainingdata_hard), 1, fp);
             if (ret != 1)
             {
-                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "2 fwrite_data %d", ret);
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_hard error! %d", ret);
             }
         }
+        fseek(fp, 0, SEEK_END);
+        ret = fwrite((void *)&data, sizeof(Trainingdata), 1, fp);
+        if (ret != 1)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_data error! %d", ret);
+        }
 #endif
-            fseek(fp, 0, SEEK_SET);
-            fclose(fp);
-            return 1;
+
+            fclose(fp); // Close the file after writing data
+            return 0;   // Return success code
         }
         // 读数据
         Trainingdata *read_Trainingdata_data(char *timedata, uint16_t offset)
@@ -953,6 +937,7 @@ namespace OHOS
             if (fp == NULL || get_Trainingdata_data_num(fp) < offset)
             {
                 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "file is NULL %d", get_Trainingdata_data_num(fp));
+                free(data); // 修复内存泄漏
                 return NULL;
             }
 #if HARD_OFF
index e76735b..4ab8bc3 100644 (file)
@@ -1,3 +1,9 @@
+/*** 
+ * @Date: 2024-04-15 10:55:58
+ * @LastEditors: Caisking 3449750381@qq.com
+ * @LastEditTime: 2024-04-29 17:02:28
+ * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/traing/training_list_fitness/include/TraingFitnessModel.h
+ */
 
 #ifndef TraingFitness_MODEL_H
 #define TraingFitness_MODEL_H
@@ -25,7 +31,7 @@ namespace OHOS
         TraingFitnessModel(const TraingFitnessModel &);
         TraingFitnessModel &operator=(const TraingFitnessModel &);
         virtual ~TraingFitnessModel();
-        static const uint16_t TraingFitnessListMaxNum = 58;
+        static const uint16_t TraingFitnessListMaxNum = 57;
         TraingFitnessItem TraingFitnesss[TraingFitnessListMaxNum] = {VIEW_INVALID, STR_EXERCISE_GENERAL};
         // std::map<int, AppViewId> TraingFitnessIdToViewId;
         bool btConnectStatus{false};
index 1747979..2a1d164 100644 (file)
@@ -142,7 +142,7 @@ namespace OHOS
         TraingFitnesss[54] = {VIEW_TRAININGNOMAL, text[54]};
         TraingFitnesss[55] = {VIEW_TRAININGNOMAL, text[55]};
         TraingFitnesss[56] = {VIEW_TRAININGNOMAL, text[56]};
-        TraingFitnesss[57] = {VIEW_TRAININGNOMAL, text[57]};
+        //访问的数组超过了数组容量
     }
 
     const TraingFitnessItem *TraingFitnessModel::GetTraingFitnesslistItems() const
index bd9effd..c781f1f 100755 (executable)
@@ -1044,7 +1044,7 @@ extern "C"
             *info = NULL;
             *num = 0;
             fclose(fp);
-            return OHOS_SUCCESS;
+            return OHOS_FAILURE;//文件为空,返回失败
         }
 
         number = fileSize / sizeof(CallLogInfo);