解决训练数据泄漏 462/1
authorCaisking <3449750381@qq.com>
Fri, 5 Jul 2024 02:34:46 +0000 (10:34 +0800)
committerCaisking <3449750381@qq.com>
Fri, 5 Jul 2024 02:34:46 +0000 (10:34 +0800)
code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp

index 0fd0daf..9c9f59b 100644 (file)
@@ -722,11 +722,11 @@ namespace OHOS
         data.cir = 0;
         data.alt = 0;
         /*配速相关还未处理 暂时保存0*/
-        data.apc=0;
-        data.fpc=0;
-        data.spc=0;
-        data.std=0;
-        data.cdc=0;
+        data.apc = 0;
+        data.fpc = 0;
+        data.spc = 0;
+        data.std = 0;
+        data.cdc = 0;
 
         // 保存数据到bin
         if (write_Trainingdata_data(data) != 1)
@@ -1112,16 +1112,23 @@ namespace OHOS
 
             FILE *fp = fopen(file_path, "rb+");
             Trainingdata *data = (Trainingdata *)malloc(sizeof(Trainingdata));
-            if (fp == NULL || get_Trainingdata_data_num(fp) <= offset)
+            if (fp == NULL)
+            {
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "file is NULL");
+                free(data); // 修复内存泄漏
+                return NULL;
+            }
+            if (get_Trainingdata_data_num(fp) <= offset)
             {
                 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "file is NULL %d", get_Trainingdata_data_num(fp));
                 free(data); // 修复内存泄漏
+                fclose(fp);
                 return NULL;
             }
             fseek(fp, sizeof(Trainingdata) * offset, SEEK_SET);
             fread((void *)data, sizeof(Trainingdata), 1, fp);
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%d", data->src);
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%d %d ", offset, sizeof(Trainingdata));
+            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "%d", data->src);
+            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "%d %d ", offset, sizeof(Trainingdata));
             fseek(fp, 0, SEEK_SET);
 
             fclose(fp);