解决内存泄漏
authorxue <2144439972@qq.com>
Sat, 20 Sep 2025 10:02:49 +0000 (18:02 +0800)
committerxue <2144439972@qq.com>
Sat, 20 Sep 2025 10:02:49 +0000 (18:02 +0800)
code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdModel.h
code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdLullabyView.cpp
code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdMainView.cpp
code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdModel.cpp
code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdOtaView.cpp
code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdPresenter.cpp

index 59c5e37..ad740c9 100644 (file)
@@ -23,6 +23,7 @@ namespace OHOS
         void StopTimer(void);
         void StartTimer(uint32_t interval = 2 * 1000);//2秒
         void StartReminderHideTimer(uint32_t intervalMs = 1000);
+        void StopReminderHideTimer(void);
         void StartOtaCountdown(uint32_t startVal);
         void StopOtaCountdown(void);
         void StartCountUpTimer(uint32_t targetSeconds);
@@ -53,6 +54,7 @@ namespace OHOS
         // 对话相关函数
         bool StartRecording(std::string audioPath);
         bool StopRecording();
+        bool IsRecording() const;
         bool SendRecording();
         bool ReceiveAnswer();
         std::string GetConversationText();
index b466698..a607394 100644 (file)
@@ -126,16 +126,39 @@ namespace OHOS
     {
         if (group != nullptr)
         {
+            // 移除所有子组件
             group->RemoveAll();
+            
+            // 删除子组件
+            if (TextLabel != nullptr) { 
+                delete TextLabel; 
+                TextLabel = nullptr; 
+            }
+            if (TextLabel_Y != nullptr) { 
+                delete TextLabel_Y; 
+                TextLabel_Y = nullptr; 
+            }
+            if (RemindLabel != nullptr) { 
+                delete RemindLabel; 
+                RemindLabel = nullptr; 
+            }
+            if (RemindIcon != nullptr) { 
+                delete RemindIcon; 
+                RemindIcon = nullptr; 
+            }
+            if (RemindIcon2 != nullptr) { 
+                delete RemindIcon2; 
+                RemindIcon2 = nullptr; 
+            }
+            if (listenAnimator != nullptr) { 
+                delete listenAnimator; 
+                listenAnimator = nullptr; 
+            }
+            
+            // 最后删除group
             delete group;
             group = nullptr;
         }
-        if (TextLabel != nullptr) { delete TextLabel; TextLabel = nullptr; }
-        if (TextLabel_Y != nullptr) { delete TextLabel_Y; TextLabel_Y = nullptr; }
-        if (RemindLabel != nullptr) { delete RemindLabel; RemindLabel = nullptr; }
-        if (RemindIcon != nullptr) { delete RemindIcon; RemindIcon = nullptr; }
-        if (RemindIcon2 != nullptr) { delete RemindIcon2; RemindIcon2 = nullptr; }
-        if (listenAnimator != nullptr) { delete listenAnimator; listenAnimator = nullptr; }
     }
     bool ThunderBirdLullabyView::EnsureListenAnimator()
     {
index 562b193..7c77f43 100644 (file)
@@ -73,7 +73,7 @@ namespace OHOS
     {
         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "ThunderBirdMainView::ThunderBirdMainView");
         g_pThunderBirdMainView = this;
-        ScreenAlwaysOnCtr(OPEN_SCREEN_ALWAYS_ON); // 打开屏幕常亮
+        //ScreenAlwaysOnCtr(OPEN_SCREEN_ALWAYS_ON); // 打开屏幕常亮
         ThunderBirdModel::GetInstance()->StartTimer();
     }
 
@@ -88,7 +88,7 @@ namespace OHOS
             ScrollView_ = nullptr;
         }
         g_pThunderBirdMainView = nullptr;
-        ScreenAlwaysOnCtr(CANCEL_SCREEN_ALWAYS_ON); // 关闭屏幕常亮
+        //ScreenAlwaysOnCtr(CANCEL_SCREEN_ALWAYS_ON); // 关闭屏幕常亮
         ThunderBirdModel::GetInstance()->StopTimer();
     }
 
@@ -99,6 +99,16 @@ namespace OHOS
 
     void ThunderBirdMainView::DeInit()
     {
+        // 先清理所有group,确保UI组件正确移除
+        for (int i = 0; i < MAXVIEW; i++)
+        {
+            if (group[i] != nullptr && ScrollView_ != nullptr)
+            {
+                ScrollView_->Remove(group[i]);
+                group[i] = nullptr;
+            }
+        }
+        
         // 释放各子界面对象
         if (g_pThunderBirdOtaView)
         {
@@ -135,16 +145,6 @@ namespace OHOS
             delete g_pThunderBirdShutdownView;
             g_pThunderBirdShutdownView = nullptr;
         }
-        if (g_pThunderBirdShutdownView)
-        {
-            delete g_pThunderBirdShutdownView;
-            g_pThunderBirdShutdownView = nullptr;
-        }
-        // 置空所有分组索引,避免悬空指针
-        for (int i = 0; i < MAXVIEW; i++)
-        {
-            group[i] = nullptr;
-        }
 
         // 不再持有旧 View 实例,全部使用聚合组件
         // 先停止视频播放
@@ -170,12 +170,20 @@ namespace OHOS
 
         if (curFace != nullptr)
         {
+            // 先从父组件中移除
+            if (ScrollView_ != nullptr) {
+                ScrollView_->Remove(curFace);
+            }
             delete curFace;
             curFace = nullptr;
         }
 
         if (surfaceView_ != nullptr)
         {
+            // 先从父组件中移除
+            if (ScrollView_ != nullptr) {
+                ScrollView_->Remove(surfaceView_);
+            }
             delete surfaceView_;
             surfaceView_ = nullptr;
         }
@@ -183,6 +191,15 @@ namespace OHOS
         // 清理顶部状态UI group1 及其子控件
         if (group1 != nullptr)
         {
+            // 先从父组件中移除group1
+            if (ScrollView_ != nullptr) {
+                ScrollView_->Remove(group1);
+            }
+            
+            // 移除所有子组件
+            group1->RemoveAll();
+            
+            // 删除子组件
             if (timeLabel != nullptr)
             {
                 delete timeLabel;
@@ -221,7 +238,8 @@ namespace OHOS
                 delete chargeBatteryicon;
                 chargeBatteryicon = nullptr;
             }
-            group1->RemoveAll();
+            
+            // 最后删除group1
             delete group1;
             group1 = nullptr;
         }
@@ -307,8 +325,11 @@ namespace OHOS
 
     bool ThunderBirdMainView::SwitchVideo(const char *uri) // 切换表情接口
     {
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "ThunderBirdMainView::SwitchVideo: Switching to video: %s", uri ? uri : "null");
+        
         if (uri == nullptr || surfaceView_ == nullptr)
         {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ThunderBirdMainView::SwitchVideo: Invalid parameters");
             return false;
         }
         // 停止并销毁旧播放器
@@ -631,7 +652,7 @@ namespace OHOS
             return;
         }
         curFace->SetAutoEnable(false);
-        curFace->SetPosition(0, 0, 360, 360);
+        curFace->SetPosition(0, 0, 360, 243);
         curFace->SetResizeMode(UIImageView::COVER);
         curFace->SetStyle(STYLE_BACKGROUND_OPA, 0x00);
         //curFace->SetStyle(STYLE_BACKGROUND_COLOR, 0xffe0c7c1);
index 35006c5..2d74544 100644 (file)
      
      ThunderBirdModel::~ThunderBirdModel() 
      {
+         // 停止所有定时器
+         StopTimer();
+         StopOtaCountdown();
+         StopCountUpTimer();
+         StopReminderHideTimer();
+         
+         // 停止录音
+         if (IsRecording()) {
+             StopRecording();
+         }
+         
          // 析构时保存提醒数据
          // SaveRemindersToFile();
+         
+         // 清理字符串资源
+         ClearTextStr();
+         m_reminders.clear();
      }
  
      ThunderBirdModel *ThunderBirdModel::GetInstance()
          }
      }
 
-
-
+     void ThunderBirdModel::StopReminderHideTimer(void)
+     {
+         if (g_reminderHideTimer != nullptr)
+         {
+             osTimerStop(g_reminderHideTimer);
+             osStatus_t ret = osTimerDelete(g_reminderHideTimer);
+             if (ret != osOK)
+             {
+                 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "StopReminderHideTimer delete failed, ret = %lx!!", ret);
+             }
+             g_reminderHideTimer = nullptr;
+         }
+     }
 
     // ---------------------- OTA 倒计时定时器(5 -> 1,每步1秒,然后监控下载状态) ----------------------
      static osTimerId_t g_otaCountdownTimer = nullptr;
          
          return true; // 模拟成功
      }
- 
+
+     bool ThunderBirdModel::IsRecording() const
+     {
+         return g_isRecording;
+     }
+
      bool ThunderBirdModel::StopRecording()
      {
          WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "ThunderBirdModel::StopRecording");
index 95fd3b7..c02e757 100644 (file)
@@ -27,18 +27,23 @@ namespace OHOS
     {
         if (group != nullptr)
         {
+            // 移除所有子组件
             group->RemoveAll();
+            
+            // 删除子组件
+            if (titleLabel != nullptr) { delete titleLabel; titleLabel = nullptr; }
+            if (TextLabel != nullptr) { delete TextLabel; TextLabel = nullptr; }
+            if (LittleTextLabel != nullptr) { delete LittleTextLabel; LittleTextLabel = nullptr; }
+            if (LittleTextLabel_2 != nullptr) { delete LittleTextLabel_2; LittleTextLabel_2 = nullptr; }
+            if (RemindLabel != nullptr) { delete RemindLabel; RemindLabel = nullptr; }
+            if (RemindIcon != nullptr) { delete RemindIcon; RemindIcon = nullptr; }
+            if (RemindIcon2 != nullptr) { delete RemindIcon2; RemindIcon2 = nullptr; }
+            if (BackGround != nullptr) { delete BackGround; BackGround = nullptr; }
+            
+            // 最后删除group
             delete group;
             group = nullptr;
         }
-        if (titleLabel != nullptr) { delete titleLabel; titleLabel = nullptr; }
-        if (TextLabel != nullptr) { delete TextLabel; TextLabel = nullptr; }
-        if (LittleTextLabel != nullptr) { delete LittleTextLabel; LittleTextLabel = nullptr; }
-        if (LittleTextLabel_2 != nullptr) { delete LittleTextLabel_2; LittleTextLabel_2 = nullptr; }
-        if (RemindLabel != nullptr) { delete RemindLabel; RemindLabel = nullptr; }
-        if (RemindIcon != nullptr) { delete RemindIcon; RemindIcon = nullptr; }
-        if (RemindIcon2 != nullptr) { delete RemindIcon2; RemindIcon2 = nullptr; }
-        if (BackGround != nullptr) { delete BackGround; BackGround = nullptr; }
     }
 
     UIViewGroup *ThunderBirdOtaView::ThunderBirdOtaViewInit(void)
index d813b91..3cd7add 100644 (file)
@@ -58,6 +58,17 @@ namespace OHOS
 
     ThunderBirdPresenter::~ThunderBirdPresenter()
     {
+        // 停止所有定时器和资源
+        ThunderBirdModel::GetInstance()->StopTimer();
+        ThunderBirdModel::GetInstance()->StopOtaCountdown();
+        ThunderBirdModel::GetInstance()->StopCountUpTimer();
+        ThunderBirdModel::GetInstance()->StopReminderHideTimer();
+        
+        // 停止录音
+        if (ThunderBirdModel::GetInstance()->IsRecording()) {
+            ThunderBirdModel::GetInstance()->StopRecording();
+        }
+        
         g_pThunderBirdPresenter = nullptr;
     }