修复通话记录存在的内存泄漏 976/2
authorhcj <1224667075@qq.com>
Sat, 14 Sep 2024 09:30:04 +0000 (17:30 +0800)
committerhcj <1224667075@qq.com>
Sat, 14 Sep 2024 09:30:04 +0000 (17:30 +0800)
code/sdk/application/wearable/fd230app/nativeui/phone/include/MainPhoneView.h
code/sdk/application/wearable/fd230app/nativeui/phone/src/MainPhoneView.cpp
code/sdk/application/wearable/fd230app/nativeui/phoneservice/src/PhoneService.cpp

index 3f56576..d75c614 100644 (file)
@@ -60,7 +60,7 @@ namespace OHOS
         UIImageView *TelephoneLineImageView{nullptr}; // 线
         UILabelExt *NoCallsLabel{nullptr};            // 暂无通话
         UILabelExt *hintCallsLabel{nullptr};            // 提示
-        CallLogInfo *info{nullptr};
+        CallLogInfo *info[20]{nullptr};
         UILabelButtonExt *button_calllog[CALL_LOG_MAX_NUM]{nullptr};
         UILabelExt *button_calllog_time[CALL_LOG_MAX_NUM]{nullptr};         //"时间"
         UILabelExt *button_calllog_status[CALL_LOG_MAX_NUM]{nullptr};         //"通话状态"
index 89ed3a1..eaf329c 100644 (file)
@@ -112,12 +112,16 @@ namespace OHOS
                     button_calllog_status[i] = nullptr;
                 }
             }
-            if(info)
+            for(int i=0;i<20;i++)
             {
-                delete info;
-                info=nullptr;
-            }
+                if(info[i])
+                {
+                    UIFree(info[i]);
+                    info[i]=nullptr;
+                }
 
+            }
+           
             if(hintCallsLabel)
             {
                 delete hintCallsLabel;
@@ -265,8 +269,8 @@ namespace OHOS
 
             
             /* ------------------------------ NoCallsLabel ------------------------------ */
-            info = new CallLogInfo[sizeof(CallLogInfo) * CALL_LOG_MAX_NUM];
-            if (GetCallLog(&info, &num) == -1) {
+            // info = new CallLogInfo[sizeof(CallLogInfo) * CALL_LOG_MAX_NUM];
+            if (GetCallLog(info, &num) == -1) {
                 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CallerLogModel::GetInstance()->ListIsEmpty");
                 return;
             }
@@ -298,7 +302,7 @@ namespace OHOS
                     //WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%s",(info + i - 1)->tel);
                     char buf[20]={0};
     
-                    sprintf_s(buf,20,"%s",(const char*)(info + i - 1)->tel);
+                    sprintf_s(buf,20,"%s",(const char*)(info[i-1])->tel);
                     char name[30]={0};
                     if(get_phone_addrss_name(name,buf,NULL))
                     {
@@ -307,7 +311,7 @@ namespace OHOS
                     {
                         buttonconfig.str=buf;
                     }
-                    buttonconfig.ViewId=(const char*)(info + i - 1)->tel;
+                    buttonconfig.ViewId=(const char*)(info[i-1])->tel;
                     button_calllog[i]=SetTheButton_ext(buttonconfig,ButtonOnclickListen);
                     if(button_calllog[i]==nullptr)
                     {
@@ -319,7 +323,7 @@ namespace OHOS
                     
                     time_t currtime;
                     struct tm *timeinfo;
-                    currtime=(info + i - 1)->creationTime;
+                    currtime=(info[i-1])->creationTime;
                     timeinfo = localtime(&currtime);
                     char time_buf[20]={0};
 
@@ -350,13 +354,13 @@ namespace OHOS
                     {
                         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MainPhoneView::MainPhoneViewTitleLabel new fail!");
                     }
-                     if((info + i - 1)->status==CALL_MISSED)
+                     if((info [i-1])->status==CALL_MISSED)
                     {
                         //labelParam.text="未接来电";
                         button_calllog_status[i]->SetTextId(STR_MISSED_CALLS);
                         button_calllog_status[i]->SetTextColor(Color::GetColorFromRGBA(0xff, 0x5a, 0x36, 0xff));
                     }
-                    else if((info + i - 1)->status==CALL_INCOMING)
+                    else if((info[i-1])->status==CALL_INCOMING)
                     {
                         //labelParam.text="拨入电话";
                         button_calllog_status[i]->SetTextId(STR_RECEIVED_CALLS);
index c0bdf7d..e654b8d 100644 (file)
@@ -36,6 +36,7 @@
 #include "ConfigModel.h"
 #include "custom_runtime.h"
 #include "vibrator_service.h"
+#include "gfx_utils/mem_api.h"
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -974,7 +975,7 @@ int GetCallLog(CallLogInfo **info, int *num)
 
     int number = fileSize / sizeof(CallLogInfo);
     WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[GetCallLog] fileSize:%d, number:%d", fileSize, number);
-    CallLogInfo *callInfo = (CallLogInfo *)malloc(fileSize);
+    CallLogInfo *callInfo = (CallLogInfo *)OHOS::UIMalloc(fileSize);
     if (callInfo == nullptr) {
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[GetCallLog] malloc callInfo fail!");
         *info = nullptr;
@@ -999,6 +1000,43 @@ int GetCallLog(CallLogInfo **info, int *num)
     return OHOS_SUCCESS;
 }
 
+
+// int SaveCallLog(CallLogInfo **info)
+// {
+//     FILE *fp = fopen(CALL_LOG_PATH, "wb+");
+//     if (fp == nullptr) {
+//         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[AddToCallLog] fopen %s failed!", CALL_LOG_PATH);
+//         return OHOS_FAILURE;
+//     }
+
+
+//     int number = fileSize / sizeof(CallLogInfo);
+//     WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[GetCallLog] fileSize:%d, number:%d", fileSize, number);
+//     CallLogInfo *callInfo = (CallLogInfo *)malloc(fileSize);
+//     if (callInfo == nullptr) {
+//         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[GetCallLog] malloc callInfo fail!");
+//         *info = nullptr;
+//         *num = 0;
+//         fclose(fp);
+//         return OHOS_FAILURE;
+//     }
+//     (void)memset_s(callInfo, fileSize, 0, fileSize);
+
+//     int ret = fread(callInfo, sizeof(CallLogInfo), number, fp);
+//     if (ret != number) {
+//         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[GetCallLog] fread fp fail ret:%d", ret);
+//         *info = nullptr;
+//         *num = 0;
+//         fclose(fp);
+//         return OHOS_FAILURE;
+//     }
+
+//     *info = callInfo;
+//     *num = number;
+//     fclose(fp);
+//     return OHOS_SUCCESS;
+// }
+
 // 清空通话记录
 int ClearCallLogFile(void)
 {