优化活动界面内存泄漏
authorhcj <1224667075@qq.com>
Fri, 30 Aug 2024 08:36:19 +0000 (16:36 +0800)
committerhcj <1224667075@qq.com>
Fri, 30 Aug 2024 08:36:19 +0000 (16:36 +0800)
code/sdk/application/wearable/fd230app/nativeui/goal/include/Maingoalpresenter.h
code/sdk/application/wearable/fd230app/nativeui/goal/include/goalview.h
code/sdk/application/wearable/fd230app/nativeui/goal/src/Maingoalpresenter.cpp
code/sdk/application/wearable/fd230app/nativeui/goal/src/Maingoalview.cpp
code/sdk/application/wearable/fd230app/nativeui/goal/src/goalother.cpp
code/sdk/application/wearable/fd230app/nativeui/goal/src/goalview.cpp
code/sdk/application/wearable/service/msg_center/src/notify/charge_notification.cpp

index 4200ed9..1a4c3a8 100644 (file)
@@ -50,7 +50,7 @@ namespace OHOS
         bool OnKeyAct(UIView &view, const KeyEvent &event) override; // 增加按键事件
         void OnSwipe(UISwipeView &view) override;
 
-        GoalModel *weatherModel{nullptr};
+        GoalModel *GoalModelModel{nullptr};
         bool X_DRAG_FLAG{false};
     };
 } // namespace OHOS
index f5ed139..de286d3 100644 (file)
@@ -39,14 +39,16 @@ namespace OHOS
         void ClearAll();
         bool GetInitViewState(void);
         void GoalViewrefresh();
+        ImageAnimatorInfo PullUpimageAnimatorInfo[70]{0};
 
     private:
         UILabelExt *InitLabelExt(int x, int y, int w, int h, const char *text, int textfont, int lineheight);
 
     private:
-        const char **CreateImagePathsArray(const char *basePath, const char *fileExtension, int numImages);
+        void CreateImagePathsArray(const char **,const char *basePath, const char *fileExtension, int numImages);
         void FreeImagePathsArray(const char **imagePaths, const int numImages);
 
+
     private:
         UICircleProgress * circleProgress1_{nullptr};   //步数进度条
 
@@ -56,8 +58,10 @@ namespace OHOS
         UIImageView* circleProgressimage{nullptr};      
         UIImageAnimatorView *PullUpAnimatorView{nullptr};   // 上拉动画
         UIImageAnimatorView *stepAnimatorView{nullptr};   // 走路动画
-        const char **PullUpPathsArray{nullptr};
-        const char **goalPathsArray{nullptr};
+        ImageAnimatorInfo goalAnimatorInfo[20]{0};
+        
+        const char *PullUpPathsArray[70]{nullptr};
+        const char *goalPathsArray[20]{nullptr};
         bool viewInitStatus{false};
     };
 } // namespace OHOS
index 71028c6..25151e8 100644 (file)
@@ -15,7 +15,7 @@ namespace OHOS
 
     MaingoalPresenter::MaingoalPresenter()
     {
-        weatherModel = &GoalModel::GetInstance();
+        GoalModelModel = &GoalModel::GetInstance();
         g_pMainWeatherPresenter = this;
 #ifdef MEM_CHECK
         MemCheck::GetInstance()->EnableLeakCheck(true);
@@ -25,6 +25,7 @@ namespace OHOS
     MaingoalPresenter::~MaingoalPresenter()
     {
         g_pMainWeatherPresenter = nullptr;
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MaingoalPresenter::~MaingoalPresenter");
 #ifdef MEM_CHECK
         MemCheck::GetInstance()->EnableLeakCheck(false);
 #endif
index 8a1cebb..c4e5ee2 100644 (file)
@@ -19,7 +19,7 @@ namespace OHOS
 
     MaingoalrView::~MaingoalrView()
     {
-        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MaingoalrView::~MaingoalrView");
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MaingoalrView::~MaingoalrView");
         ClearAll();
         g_pgoalView = nullptr;
     }
@@ -31,12 +31,9 @@ namespace OHOS
 
     void MaingoalrView::ClearAll(void)
     {
-        if (goalSwipeViewList != nullptr)
+      
+        if(goalSwipeViewList)
         {
-            for (int32_t i = 0; i < maxPage; i++)
-            {
-                recordCard[i] = nullptr;
-            }
             goalSwipeViewList->RemoveAll();
 
             delete goalView;
@@ -48,10 +45,16 @@ namespace OHOS
             delete goalhintView;
             goalhintView = nullptr;
 
-          
+            
             delete goalSwipeViewList;
             goalSwipeViewList = nullptr;
+
+            for (int32_t i = 0; i < 3; i++)
+            {
+                recordCard[i] = nullptr;
+            }
         }
+        
 
     }
 
@@ -89,7 +92,6 @@ namespace OHOS
         return goalSwipeViewList;
     }
 
-    // 获取天气页面视图
     UIView *MaingoalrView::GetGoalPageView()
     {
        
@@ -149,9 +151,12 @@ namespace OHOS
 
     void MaingoalrView::OnStop()
     {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MaingoalrView OnStop!!");
+
         View<MaingoalPresenter>::OnStop();
         RootView::GetInstance()->ClearOnKeyActListener();
         RootView::GetInstance()->RemoveAll();
+
         return;
     }
 
@@ -176,7 +181,6 @@ namespace OHOS
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MaingoalrView::new goalView fail!");
             return nullptr;
         }
-
         return goalView;
     }
 
@@ -200,7 +204,6 @@ namespace OHOS
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MaingoalrView::new goalView fail!");
             return nullptr;
         }
-
         return goalhintView;
     }
 
@@ -231,12 +234,13 @@ namespace OHOS
         AppGroupView *view = nullptr;
         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CreatePage WeatherPageId:%d", WeatherPageId);
 
-        for (i = 0; i < sizeof(g_pageMapper) / sizeof(g_pageMapper[0]); i++)
+        for (i = 0; i < 3;i++)
+        //sizeof(g_pageMapper) / sizeof(g_pageMapper[0]); i++)
         {
             if (g_pageMapper[i].viewId == WeatherPageId)
             {                                             // 正确使用 WeatherPageId 来匹配视图ID
                 view = (this->*(g_pageMapper[i].func))(); // 如果找到匹配项,则调用相应的函数创建视图
-                WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CreatePage view:%p", view);
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CreatePage view:%p", view);
                 return view; // 返回创建的视图
             }
         }
index 890f64e..a03c335 100644 (file)
@@ -2,6 +2,7 @@
 #include "ConfigModel.h"
 #include "goal/include/goalmodel.h"
 #include "common/image_cache_manager.h"
+#include "goal/include/goalview.h"
 /*
 当至天气界面时,主动获取最新天气数据。
 (此操作静默将进行,获取到则立刻更新显示,获取不到则重试
@@ -19,7 +20,7 @@ namespace OHOS
     static constexpr int16_t PULLUP_ANIMATOR_VIEW_H = 40;
     static constexpr int16_t PULLUP_ANIMATOR_VIEW_NUM_FRAMES = 70;
     static constexpr int16_t PULLUP_ANIMATOR_VIEW_TIME = 100;
-    static ImageAnimatorInfo PullUpimageAnimatorInfo[PULLUP_ANIMATOR_VIEW_NUM_FRAMES];
+    //static ImageAnimatorInfo PullUpimageAnimatorInfo[PULLUP_ANIMATOR_VIEW_NUM_FRAMES];
 
     static ImageAnimatorInfo goalAnimatorInfo[20];
     static constexpr const char *PullUpPaths = "/user/res/weather/PullUp/PullUp_";
@@ -44,16 +45,21 @@ namespace OHOS
 
     void GoalotherView::GoalViewrefresh()
     {
+        if(mileagenowdataLabel)
         mileagenowdataLabel->SetText(GoalModel::GetInstance().getdistancenew());
+        if(mileagegoaldataLabel)
         mileagegoaldataLabel->SetText(GoalModel::GetInstance().getdistanceGoal());
-
+        if(fatburningnowdataLabel)
         fatburningnowdataLabel->SetText(GoalModel::GetInstance().getfatburningnew());
+        if(fatburninggoaldataLabel)
         fatburninggoaldataLabel->SetText(GoalModel::GetInstance().getfatburninggoal());
-
+        if(standnowdataLabel)
         standnowdataLabel->SetText(GoalModel::GetInstance().getstandnew());
+        if(standgoaldataLabel)
         standgoaldataLabel->SetText(GoalModel::GetInstance().getstandgoal());
-
+        if(calorienowdataLabel)
         calorienowdataLabel->SetText(GoalModel::GetInstance().getcaloriespnew());
+        if(caloriegoaldataLabel)
         caloriegoaldataLabel->SetText(GoalModel::GetInstance().getcaloriesGoal());
 
         Invalidate();
@@ -153,7 +159,7 @@ namespace OHOS
             delete PullUpAnimatorView;
             PullUpAnimatorView = nullptr;
         }
-        FreeImagePathsArray(PullUpPathsArray, PULLUP_ANIMATOR_VIEW_NUM_FRAMES);
+        //FreeImagePathsArray(PullUpPathsArray, PULLUP_ANIMATOR_VIEW_NUM_FRAMES);
 
     }
 
@@ -425,7 +431,6 @@ namespace OHOS
         caloriegoaldataLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 40);
         caloriegoaldataLabel->SetVisible(true);
 
-        PullUpPathsArray = CreateImagePathsArray(PullUpPaths, "bin", PULLUP_ANIMATOR_VIEW_NUM_FRAMES);
         if (PullUpAnimatorView == nullptr)
         {
             PullUpAnimatorView = new UIImageAnimatorView();
@@ -434,19 +439,25 @@ namespace OHOS
                 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "PullUpAnimatorView new fail");
                 return false;
             }
-            for (uint8_t i = 0; i < PULLUP_ANIMATOR_VIEW_NUM_FRAMES; i++)
+            if(GoalView::GetInstance()==nullptr)
             {
-                PullUpimageAnimatorInfo[i].imageInfo = ImageCacheManager::GetInstance().LoadSingleRes(PullUpPathsArray[i]);
-                PullUpimageAnimatorInfo[i].imageType = IMG_SRC_IMAGE_INFO;
-                PullUpimageAnimatorInfo[i].pos.x = PULLUP_ANIMATOR_VIEW_X;
-                PullUpimageAnimatorInfo[i].pos.y = PULLUP_ANIMATOR_VIEW_Y;
-                PullUpimageAnimatorInfo[i].width = PULLUP_ANIMATOR_VIEW_W;
-                PullUpimageAnimatorInfo[i].height = PULLUP_ANIMATOR_VIEW_H;
+                // PullUpPathsArray = CreateImagePathsArray(PullUpPaths, "bin", PULLUP_ANIMATOR_VIEW_NUM_FRAMES);
+                // for (uint8_t i = 0; i < PULLUP_ANIMATOR_VIEW_NUM_FRAMES; i++)
+                // {
+                //     PullUpimageAnimatorInfo[i].imageInfo = ImageCacheManager::GetInstance().LoadSingleRes(PullUpPathsArray[i]);
+                //     PullUpimageAnimatorInfo[i].imageType = IMG_SRC_IMAGE_INFO;
+                //     PullUpimageAnimatorInfo[i].pos.x = PULLUP_ANIMATOR_VIEW_X;
+                //     PullUpimageAnimatorInfo[i].pos.y = PULLUP_ANIMATOR_VIEW_Y;
+                //     PullUpimageAnimatorInfo[i].width = PULLUP_ANIMATOR_VIEW_W;
+                //     PullUpimageAnimatorInfo[i].height = PULLUP_ANIMATOR_VIEW_H;
+                // }
+                // PullUpAnimatorView->SetPosition(PULLUP_ANIMATOR_VIEW_X, PULLUP_ANIMATOR_VIEW_Y, PULLUP_ANIMATOR_VIEW_W, PULLUP_ANIMATOR_VIEW_H);
+                // PullUpAnimatorView->SetStyle(STYLE_BACKGROUND_COLOR, 0xffffffff);
+                // PullUpAnimatorView->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
+                return false;
+
             }
-            PullUpAnimatorView->SetPosition(PULLUP_ANIMATOR_VIEW_X, PULLUP_ANIMATOR_VIEW_Y, PULLUP_ANIMATOR_VIEW_W, PULLUP_ANIMATOR_VIEW_H);
-            PullUpAnimatorView->SetStyle(STYLE_BACKGROUND_COLOR, 0xffffffff);
-            PullUpAnimatorView->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
-            PullUpAnimatorView->SetImageAnimatorSrc(PullUpimageAnimatorInfo, PULLUP_ANIMATOR_VIEW_NUM_FRAMES, PULLUP_ANIMATOR_VIEW_TIME);
+            PullUpAnimatorView->SetImageAnimatorSrc(GoalView::GetInstance()->PullUpimageAnimatorInfo, PULLUP_ANIMATOR_VIEW_NUM_FRAMES, PULLUP_ANIMATOR_VIEW_TIME);
             PullUpAnimatorView->SetRepeat(true);
             PullUpAnimatorView->Start();
         }
@@ -518,9 +529,12 @@ namespace OHOS
         for (int i = 0; i < numImages; i++)
         {
             ImageCacheManager::GetInstance().UnloadSingleRes(imagePaths[i]);
-            delete imagePaths[i];
+            if(imagePaths[i])
+            free( (void *)imagePaths[i]);
             imagePaths[i] = nullptr;
         }
+        delete imagePaths;
+        imagePaths = nullptr;
     }
 
 }
\ No newline at end of file
index d0752e1..0625c27 100644 (file)
@@ -2,6 +2,7 @@
 #include "ConfigModel.h"
 #include "goal/include/goalmodel.h"
 #include "common/image_cache_manager.h"
+#include "gfx_utils/mem_api.h"
 /*
 当至天气界面时,主动获取最新天气数据。
 (此操作静默将进行,获取到则立刻更新显示,获取不到则重试
@@ -19,9 +20,8 @@ namespace OHOS
     static constexpr int16_t PULLUP_ANIMATOR_VIEW_H = 40;
     static constexpr int16_t PULLUP_ANIMATOR_VIEW_NUM_FRAMES = 70;
     static constexpr int16_t PULLUP_ANIMATOR_VIEW_TIME = 100;
-    static ImageAnimatorInfo PullUpimageAnimatorInfo[PULLUP_ANIMATOR_VIEW_NUM_FRAMES];
-
-    static ImageAnimatorInfo goalAnimatorInfo[20];
+   
+    
     static constexpr const char *PullUpPaths = "/user/res/weather/PullUp/PullUp_";
 
     static constexpr const char *stepPaths = "/user/res/step/";
@@ -45,8 +45,11 @@ namespace OHOS
 
     void GoalView::GoalViewrefresh()
     {
+        if(nowstepLabel)
         nowstepLabel->SetText(GoalModel::GetInstance().getstepnew());
+        if(goalstepLabel)
         goalstepLabel->SetText(GoalModel::GetInstance().getstepGoal());
+        if(circleProgress1_)
         circleProgress1_->SetValue(GoalModel::GetInstance().getstepvalue());
 
         Invalidate();
@@ -81,6 +84,7 @@ namespace OHOS
         if (stepAnimatorView != nullptr)
         {
             stepAnimatorView->Stop();
+            //stepAnimatorView->ClearImagesCache();
             delete stepAnimatorView;
             stepAnimatorView = nullptr;
         }
@@ -95,9 +99,21 @@ namespace OHOS
             delete circleProgressimage;
             circleProgressimage = nullptr;
         }
-        
+        // for(int i=0;i<20;i++)
+        // {
+            
+        // }
+        ImageCacheManager::GetInstance().UnloadSingleRes("/user/res/step/step_b.bin");
+
         FreeImagePathsArray(PullUpPathsArray, PULLUP_ANIMATOR_VIEW_NUM_FRAMES);
         FreeImagePathsArray(goalPathsArray, 20);
+
+        // if(goalAnimatorInfo)
+        // {
+
+        //     delete goalAnimatorInfo;
+        //     goalAnimatorInfo= nullptr;
+        // }
         
     }
 
@@ -127,8 +143,7 @@ namespace OHOS
     // 初始化天气页面
     bool GoalView::InitPage(void)
     {
-       
-
+    
         /* --------------------------------goalTitle--------------------------------- */
          if (goalTitle == nullptr) {
             goalTitle = new UILabelExt();
@@ -198,8 +213,8 @@ namespace OHOS
 
      
         char* image5imagesPath = "/user/res/step/step_b.bin";
-
-        circleProgressimage->SetSrc(image5imagesPath);
+        ImageInfo *info= ImageCacheManager::GetInstance().LoadSingleRes(image5imagesPath);
+        circleProgressimage->SetSrc(info);
 
             // Add to rootView
       
@@ -244,7 +259,7 @@ namespace OHOS
         circleProgress1_->SetCapType(CAP_ROUND);
 
          /*----------------------------------------------------------------------------- */
-         goalPathsArray = CreateImagePathsArray(stepPaths, "bin", 20);
+        CreateImagePathsArray(goalPathsArray,stepPaths, "bin", 20);
         if (stepAnimatorView == nullptr)
         {
             stepAnimatorView = new UIImageAnimatorView();
@@ -257,24 +272,33 @@ namespace OHOS
             {
                 //goalAnimatorInfo[i].imageInfo = ImageCacheManager::GetInstance().LoadSingleRes(goalPathsArray[i]);
                 //goalAnimatorInfo[i].imageType = IMG_SRC_IMAGE_INFO;
-                goalAnimatorInfo[i].imagePath = goalPathsArray[i];
-                goalAnimatorInfo[i].imageType = IMG_SRC_FILE_PATH;
+                //goalAnimatorInfo=new ImageAnimatorInfo [20];
+                //goalAnimatorInfo[i].imageInfo=nullptr;
+                goalAnimatorInfo[i].imageInfo = ImageCacheManager::GetInstance().LoadSingleRes(goalPathsArray[i]);
+                goalAnimatorInfo[i].imageType = IMG_SRC_IMAGE_INFO;
+
+                //goalAnimatorInfo[i].imagePath = goalPathsArray[i];
+                //goalAnimatorInfo[i].imageInfo=nullptr;
+                //goalAnimatorInfo[i].imageType = IMG_SRC_FILE_PATH;
                 goalAnimatorInfo[i].pos.x = 193;
                 goalAnimatorInfo[i].pos.y = 90;
                 goalAnimatorInfo[i].width = 100;
                 goalAnimatorInfo[i].height = 100;
+                
             }
             stepAnimatorView->SetPosition(183, 90, 100, 100);
             stepAnimatorView->SetStyle(STYLE_BACKGROUND_COLOR, 0xffffffff);
             stepAnimatorView->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
+            //这里出现内存泄露
             stepAnimatorView->SetImageAnimatorSrc(goalAnimatorInfo, 20, 50);
+
             stepAnimatorView->SetRepeat(true);
             stepAnimatorView->Start();
         }
 
 
 
-        PullUpPathsArray = CreateImagePathsArray(PullUpPaths, "bin", PULLUP_ANIMATOR_VIEW_NUM_FRAMES);
+       CreateImagePathsArray( PullUpPathsArray,PullUpPaths, "bin", PULLUP_ANIMATOR_VIEW_NUM_FRAMES);
         if (PullUpAnimatorView == nullptr)
         {
             PullUpAnimatorView = new UIImageAnimatorView();
@@ -287,8 +311,9 @@ namespace OHOS
             {
                 //PullUpimageAnimatorInfo[i].imageInfo = ImageCacheManager::GetInstance().LoadSingleRes(PullUpPathsArray[i]);
                 //PullUpimageAnimatorInfo[i].imageType = IMG_SRC_IMAGE_INFO;
-                PullUpimageAnimatorInfo[i].imagePath = PullUpPathsArray[i];
-                PullUpimageAnimatorInfo[i].imageType = IMG_SRC_FILE_PATH;
+                PullUpimageAnimatorInfo[i].imageInfo =  ImageCacheManager::GetInstance().LoadSingleRes(PullUpPathsArray[i]);
+                //PullUpPathsArray[i];
+                PullUpimageAnimatorInfo[i].imageType = IMG_SRC_IMAGE_INFO;
                 PullUpimageAnimatorInfo[i].pos.x = PULLUP_ANIMATOR_VIEW_X;
                 PullUpimageAnimatorInfo[i].pos.y = PULLUP_ANIMATOR_VIEW_Y;
                 PullUpimageAnimatorInfo[i].width = PULLUP_ANIMATOR_VIEW_W;
@@ -337,17 +362,20 @@ namespace OHOS
     }
    
 
-    const char **GoalView::CreateImagePathsArray(const char *basePath, const char *fileExtension, int numImages)
+    void GoalView::CreateImagePathsArray(const char **imagePaths,const char *basePath, const char *fileExtension, int numImages)
     {
-        const char **imagePaths = new const char *[numImages];
+        //const char **imagePaths = new const char *[numImages];
         char buffer[256];
         for (int i = 0; i < numImages; i++)
         {
+            imagePaths[i]=(char *)UIMalloc(128);
+            memset_s((void *)imagePaths[i],128,0,128);
             snprintf(buffer, sizeof(buffer), "%s%d.%s", basePath, i + 1, fileExtension);
-            imagePaths[i] = strdup(buffer);
-            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, " imagePaths[%d] = %s", i, imagePaths[i]);
+            memcpy_s((void*)imagePaths[i],128,(void*)buffer,128);
+            // imagePaths[i] = strdup(buffer);
+            //WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " imagePaths[%d] = %s", i, imagePaths[i]);
         }
-        return imagePaths;
+        return ;
     }
 
     /*
@@ -360,9 +388,14 @@ namespace OHOS
         for (int i = 0; i < numImages; i++)
         {
             //ImageCacheManager::GetInstance().UnloadSingleRes(imagePaths[i]);
-            delete imagePaths[i];
-            imagePaths[i] = nullptr;
+            ImageCacheManager::GetInstance().UnloadSingleRes(imagePaths[i]);
+
+            if(imagePaths[i])
+            UIFree((void*)imagePaths[i]);
+            //imagePaths[i] = nullptr;
         }
+        // delete[] imagePaths;
+        // imagePaths=nullptr;
     }
 
 }
\ No newline at end of file
index 2040067..87f2fa7 100644 (file)
@@ -522,7 +522,7 @@ namespace OHOS
             alarm_UILabel->SetLineBreakMode(UILabel::LINE_BREAK_WRAP);
             alarm_UILabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
             alarm_UILabel->SetRollSpeed(0);
-            alarm_UILabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 33);
+            alarm_UILabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30);
             osStatus_t retTimer;
             low_power_timer = osTimerNew((osTimerFunc_t)low_power_TimerCallBack, osTimerPeriodic, NULL, NULL);
             retTimer = osTimerStart(low_power_timer, 1000*1);
@@ -554,7 +554,7 @@ namespace OHOS
         // Set additional property
         alarm_labelbutton->SetText("确认");
         alarm_labelbutton->SetTextId(STR_KONW);
-        alarm_labelbutton->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 44);
+        alarm_labelbutton->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30);
         alarm_labelbutton->SetStyleForState(STYLE_BORDER_COLOR, 0xff0b1928, UIButton::PRESSED);
 
         UIView::OnClickListener *button1Click = static_cast<UIView::OnClickListener *>(lowppowrViewEventListener::GetInstance());