fix: memory leaks and uninitialized pointers
- Initialize _mqtt_bridge and _web_config to nullptr in declarations - Add destructor to clean up dynamically allocated objects - Initialize _last_mqtt_stats to 0 in declaration - Fix WiFiState/NetworkState enum mismatch in getWiFiStatus() While embedded firmware typically runs forever (making these not critical leaks), proper cleanup enables testing and prevents static analyzer warnings.
This commit is contained in:
parent
7516c808a7
commit
669ef89a66
@ -733,6 +733,21 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
|
||||
_prefs.adc_multiplier = 0.0f; // 0.0f means use default board multiplier
|
||||
}
|
||||
|
||||
MyMesh::~MyMesh() {
|
||||
// Clean up dynamically allocated resources
|
||||
#ifdef WITH_MQTT
|
||||
delete _web_config;
|
||||
_web_config = nullptr;
|
||||
delete _mqtt_bridge;
|
||||
_mqtt_bridge = nullptr;
|
||||
#endif
|
||||
|
||||
#ifdef WITH_ETHERNET
|
||||
delete _mqtt_bridge;
|
||||
_mqtt_bridge = nullptr;
|
||||
#endif
|
||||
}
|
||||
|
||||
void MyMesh::begin(FILESYSTEM *fs) {
|
||||
mesh::Mesh::begin();
|
||||
_fs = fs;
|
||||
@ -1289,9 +1304,9 @@ const char* MyMesh::getMQTTStatus() const {
|
||||
|
||||
const char* MyMesh::getWiFiStatus() const {
|
||||
switch (_wifi_mgr.getState()) {
|
||||
case WiFiState::CONNECTED: return "connected";
|
||||
case WiFiState::CONNECTING: return "connecting";
|
||||
case WiFiState::AP_MODE: return "ap_mode";
|
||||
case NetworkState::CONNECTED: return "connected";
|
||||
case NetworkState::CONNECTING: return "connecting";
|
||||
case NetworkState::AP_MODE: return "ap_mode";
|
||||
default: return "disconnected";
|
||||
}
|
||||
}
|
||||
|
||||
11
src/MyMesh.h
11
src/MyMesh.h
@ -125,21 +125,21 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
|
||||
#ifdef WITH_MQTT
|
||||
WiFiManager _wifi_mgr;
|
||||
MQTTBridge* _mqtt_bridge;
|
||||
WebConfig* _web_config;
|
||||
MQTTBridge* _mqtt_bridge = nullptr;
|
||||
WebConfig* _web_config = nullptr;
|
||||
WiFiConfig _wifi_config;
|
||||
MQTTConfig _mqtt_config;
|
||||
unsigned long _last_mqtt_stats;
|
||||
unsigned long _last_mqtt_stats = 0;
|
||||
|
||||
void initMQTT();
|
||||
#endif
|
||||
|
||||
#ifdef WITH_ETHERNET
|
||||
EthernetManager _eth_mgr;
|
||||
MQTTBridge* _mqtt_bridge;
|
||||
MQTTBridge* _mqtt_bridge = nullptr;
|
||||
MQTTConfig _mqtt_config;
|
||||
EthernetConfig _eth_config;
|
||||
unsigned long _last_mqtt_stats;
|
||||
unsigned long _last_mqtt_stats = 0;
|
||||
|
||||
void initEthernet();
|
||||
#endif
|
||||
@ -196,6 +196,7 @@ protected:
|
||||
|
||||
public:
|
||||
MyMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables);
|
||||
~MyMesh(); // Clean up dynamically allocated resources
|
||||
|
||||
void begin(FILESYSTEM* fs);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user