From: luozheng <1732036298@qq.com> Date: Fri, 11 Oct 2024 10:18:35 +0000 (+0800) Subject: 解决天气内存泄漏 X-Git-Tag: release_V1.2.70_20241020_135402~81 X-Git-Url: http://git.snicker.com.cn/css/html/static/gitweb.css?a=commitdiff_plain;h=602f637ce49745113730a3e914c89020580f08d3;p=mirrors%2FFD230.git 解决天气内存泄漏 --- diff --git a/code/sdk/application/wearable/fd230app/nativeui/weather/src/MainWeatherView.cpp b/code/sdk/application/wearable/fd230app/nativeui/weather/src/MainWeatherView.cpp index 69919a45f..cacc5a8b3 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/weather/src/MainWeatherView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/weather/src/MainWeatherView.cpp @@ -2,7 +2,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-01-29 15:37:44 * @LastEditors: luozheng 1732036298@qq.com - * @LastEditTime: 2024-09-03 18:01:48 + * @LastEditTime: 2024-10-11 18:12:42 * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/weather/src/MainWeatherView.cpp */ #include "weather/include/MainWeatherView.h" @@ -20,6 +20,7 @@ namespace OHOS MainWeatherView::~MainWeatherView() { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MainWeatherView::~MainWeatherView"); + ClearAll(); g_pMainWeatherView = nullptr; } @@ -30,19 +31,8 @@ namespace OHOS void MainWeatherView::ClearAll(void) { - if (container_ != nullptr) - { - container_->RemoveAll(); - delete container_; - container_ = nullptr; - } - if (weatherSwipeViewList != nullptr) { - for (int32_t i = 0; i < maxPage; i++) - { - recordCard[i] = nullptr; - } weatherSwipeViewList->RemoveAll(); delete weatherView; @@ -63,6 +53,11 @@ namespace OHOS delete getWeatherDataErrorView; getWeatherDataErrorView = nullptr; } + + for (int32_t i = 0; i < maxPage; i++) + { + recordCard[i] = nullptr; + } } // 设置按键监听 @@ -189,8 +184,12 @@ namespace OHOS { View::OnStop(); RootView::GetInstance()->ClearOnKeyActListener(); - - ClearAll(); + if (container_ != nullptr) + { + container_->RemoveAll(); + delete container_; + container_ = nullptr; + } return; } diff --git a/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherControlView.cpp b/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherControlView.cpp index 1eb0079d2..4de132004 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherControlView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherControlView.cpp @@ -2,7 +2,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-01-29 15:37:44 * @LastEditors: luozheng 1732036298@qq.com - * @LastEditTime: 2024-09-03 18:01:21 + * @LastEditTime: 2024-10-11 18:10:13 * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherControlView.cpp */ #include "weather/include/WeatherControlView.h" @@ -35,32 +35,26 @@ namespace OHOS if (viewiInitStatus == true) { RemoveAll(); - if (mainWeatherView != nullptr) - { - if (weatherOnSwipeListener != nullptr) - { - delete weatherOnSwipeListener; - weatherOnSwipeListener = nullptr; - } - - if (mainWeatherView != nullptr) - { - delete mainWeatherView; - mainWeatherView = nullptr; - } - } + delete weatherOnSwipeListener; + weatherOnSwipeListener = nullptr; + + delete mainWeatherView; + mainWeatherView = nullptr; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WeatherControlView::ClearAll success!!"); } } bool WeatherControlView::CreateMainWeatherView() { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WeatherControlView::InitView"); - - mainWeatherView = new MainWeatherView(); if (mainWeatherView == nullptr) { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::new mainWeatherView fail!"); - return false; + mainWeatherView = new MainWeatherView(); + if (mainWeatherView == nullptr) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::new mainWeatherView fail!"); + return false; + } } return true; @@ -69,18 +63,24 @@ namespace OHOS // 天气数据有效初始化该视图 bool WeatherControlView::InitializeWeatherSwipeView() { - weatherSwipView = mainWeatherView->MainWeatherViewInit(); if (weatherSwipView == nullptr) { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "mainWeatherView->MainWeatherViewInit() == nullptr!!"); - return false; + weatherSwipView = mainWeatherView->MainWeatherViewInit(); + if (weatherSwipView == nullptr) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "mainWeatherView->MainWeatherViewInit() == nullptr!!"); + return false; + } } - weatherOnSwipeListener = static_cast(new WeatherControlViewOnSwipeListener()); if (weatherOnSwipeListener == nullptr) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "weatherOnSwipeListener new fail"); - return false; + weatherOnSwipeListener = static_cast(new WeatherControlViewOnSwipeListener()); + if (weatherOnSwipeListener == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "weatherOnSwipeListener new fail"); + return false; + } } // weatherSwipView->SetOnSwipeListener(weatherOnSwipeListener); @@ -93,11 +93,14 @@ namespace OHOS // 无天气数据则初始化该视图 bool WeatherControlView::InitializeWeatherDataErrorView() { - weatherScrllView = mainWeatherView->WeatherDataErrorViewPageInit(); if (weatherScrllView == nullptr) { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "mainWeatherView->WeatherDataErrorViewPageInit() == nullptr!!"); - return false; + weatherScrllView = mainWeatherView->WeatherDataErrorViewPageInit(); + if (weatherScrllView == nullptr) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "mainWeatherView->WeatherDataErrorViewPageInit() == nullptr!!"); + return false; + } } weatherScrllView->SetVisible(true); // 默认没有天气数据