From e93ccc8f285926eea0e1178515db6a4f291cdcd7 Mon Sep 17 00:00:00 2001 From: Huang Rui Date: Thu, 18 Aug 2016 02:51:44 +0800 Subject: [PATCH] Fix memory leak when delete mqtt clientadd modules/rfinit.c, add __attribute__((weak)) to make it works on both sdk 2.0.0 and esp_open_sdk --- modules/rfinit.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ mqtt/mqtt.c | 61 +++++++++++++++++++++++++++++++------- 2 files changed, 127 insertions(+), 10 deletions(-) create mode 100644 modules/rfinit.c diff --git a/modules/rfinit.c b/modules/rfinit.c new file mode 100644 index 0000000..3f342ac --- /dev/null +++ b/modules/rfinit.c @@ -0,0 +1,76 @@ +/****************************************************************************** + * Copyright 2016 Vowstar + * + * FileName: init.c + * + * Description: System and user APP initialization. + * + * Modification history: + * 2016/03/24, v1.0 create this file. +*******************************************************************************/ + +#include "ets_sys.h" +#include "osapi.h" +#include "user_interface.h" + + /****************************************************************************** + * FunctionName : user_rf_cal_sector_set + * Description : SDK just reversed 4 sectors, used for rf init data and paramters. + * We add this function to force users to set rf cal sector, since + * we don't know which sector is free in user's application. + * sector map for last several sectors : ABCCC + * A : rf cal + * B : rf init data + * C : sdk parameters + * Parameters : none + * Returns : rf cal sector +*******************************************************************************/ +uint32 ICACHE_FLASH_ATTR __attribute__((weak)) +user_rf_cal_sector_set(void) +{ + enum flash_size_map size_map = system_get_flash_size_map(); + uint32 rf_cal_sec = 0; + + switch (size_map) { + case FLASH_SIZE_4M_MAP_256_256: + rf_cal_sec = 128 - 5; + break; + + case FLASH_SIZE_8M_MAP_512_512: + rf_cal_sec = 256 - 5; + break; + + case FLASH_SIZE_16M_MAP_512_512: + case FLASH_SIZE_16M_MAP_1024_1024: + rf_cal_sec = 512 - 5; + break; + + case FLASH_SIZE_32M_MAP_512_512: + case FLASH_SIZE_32M_MAP_1024_1024: + rf_cal_sec = 1024 - 5; + break; + + default: + rf_cal_sec = 0; + break; + } + + return rf_cal_sec; +} + +void __attribute__((weak)) +user_rf_pre_init(void) +{ + // Warning: IF YOU DON'T KNOW WHAT YOU ARE DOING, DON'T TOUCH THESE CODE + + // Control RF_CAL by esp_init_data_default.bin(0~127byte) 108 byte when wakeup + // Will low current + // system_phy_set_rfoption(0); + + // Process RF_CAL when wakeup. + // Will high current + system_phy_set_rfoption(1); + + // Set Wi-Fi Tx Power, Unit: 0.25dBm, Range: [0, 82] + system_phy_set_max_tpw(82); +} diff --git a/mqtt/mqtt.c b/mqtt/mqtt.c index 90732ff..ffedcc1 100644 --- a/mqtt/mqtt.c +++ b/mqtt/mqtt.c @@ -159,9 +159,15 @@ mqtt_tcpclient_delete(MQTT_Client *mqttClient) { if (mqttClient->pCon != NULL) { INFO("Free memory\r\n"); + // Force abort connections + espconn_abort(mqttClient->pCon); + // Delete connections espconn_delete(mqttClient->pCon); - if (mqttClient->pCon->proto.tcp) + + if (mqttClient->pCon->proto.tcp) { os_free(mqttClient->pCon->proto.tcp); + mqttClient->pCon->proto.tcp = NULL; + } os_free(mqttClient->pCon); mqttClient->pCon = NULL; } @@ -175,7 +181,13 @@ mqtt_tcpclient_delete(MQTT_Client *mqttClient) void ICACHE_FLASH_ATTR mqtt_client_delete(MQTT_Client *mqttClient) { - mqtt_tcpclient_delete(mqttClient); + if(mqttClient == NULL) + return; + + if (mqttClient->pCon != NULL){ + mqtt_tcpclient_delete(mqttClient); + } + if (mqttClient->host != NULL) { os_free(mqttClient->host); mqttClient->host = NULL; @@ -186,6 +198,29 @@ mqtt_client_delete(MQTT_Client *mqttClient) mqttClient->user_data = NULL; } + if(mqttClient->mqtt_state.in_buffer != NULL) { + os_free(mqttClient->mqtt_state.in_buffer); + mqttClient->mqtt_state.in_buffer = NULL; + } + + if(mqttClient->mqtt_state.out_buffer != NULL) { + os_free(mqttClient->mqtt_state.out_buffer); + mqttClient->mqtt_state.out_buffer = NULL; + } + + if(mqttClient->mqtt_state.outbound_message != NULL) { + if(mqttClient->mqtt_state.outbound_message->data != NULL) + { + os_free(mqttClient->mqtt_state.outbound_message->data); + mqttClient->mqtt_state.outbound_message->data = NULL; + } + } + + if(mqttClient->mqtt_state.mqtt_connection.buffer != NULL) { + // Already freed but not NULL + mqttClient->mqtt_state.mqtt_connection.buffer = NULL; + } + if(mqttClient->connect_info.client_id != NULL) { os_free(mqttClient->connect_info.client_id); mqttClient->connect_info.client_id = NULL; @@ -211,15 +246,12 @@ mqtt_client_delete(MQTT_Client *mqttClient) mqttClient->connect_info.will_message = NULL; } - if(mqttClient->mqtt_state.in_buffer != NULL) { - os_free(mqttClient->mqtt_state.in_buffer); - mqttClient->mqtt_state.in_buffer = NULL; + if (mqttClient->msgQueue.buf != NULL) { + os_free(mqttClient->msgQueue.buf); + mqttClient->msgQueue.buf = NULL; } - if(mqttClient->mqtt_state.out_buffer != NULL) { - os_free(mqttClient->mqtt_state.out_buffer); - mqttClient->mqtt_state.out_buffer = NULL; - } + mqttClient->connState = WIFI_INIT; } @@ -843,7 +875,16 @@ MQTT_Disconnect(MQTT_Client *mqttClient) void ICACHE_FLASH_ATTR MQTT_DeleteClient(MQTT_Client *mqttClient) { - mqttClient->connState = MQTT_DELETING; + if(NULL == mqttClient) + return; + + mqttClient->connState = MQTT_DELETED; + // if(TCP_DISCONNECTED == mqttClient->connState) { + // mqttClient->connState = MQTT_DELETED; + // } else if(MQTT_DELETED != mqttClient->connState) { + // mqttClient->connState = MQTT_DELETING; + // } + system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)mqttClient); os_timer_disarm(&mqttClient->mqttTimer); }