【随笔】cJSON内存泄露问题

在网上搜到cJSON通过cJSON_CreateObject()来添加节点,其实这样是错误的,这样会使得该根部节点下创建的节点无法释放内存。
`
void update_info(void)
{
cJSON* Data_ReadytoSend=NULL;
cJSON* DR_Data=NULL;
cJSON* Sensitivity_data=NULL;
cJSON* Version_data=NULL;
char* data_buffer;

Data_ReadytoSend = cJSON_CreateObject();
DR_Data = cJSON_CreateObject();
Sensitivity_data = cJSON_CreateObject();
Version_data = cJSON_CreateObject();

cJSON_AddItemToObject(Data_ReadytoSend, "Digital Potentiometer", DR_Data);
cJSON_AddNumberToObject(DR_Data, "CH1", Settings.Register1_RADC1);
cJSON_AddNumberToObject(DR_Data, "CH2", Settings.Register1_RADC2);
cJSON_AddNumberToObject(DR_Data, "CH3", Settings.Register2_RADC1);
cJSON_AddNumberToObject(DR_Data, "CH4", Settings.Register2_RADC2);
cJSON_AddNumberToObject(DR_Data, "CH5", Settings.Register3_RADC1);
cJSON_AddNumberToObject(DR_Data, "CH6", Settings.Register3_RADC2);
cJSON_AddNumberToObject(DR_Data, "CH7", Settings.Register4_RADC1);
cJSON_AddNumberToObject(DR_Data, "CH8", Settings.Register4_RADC2);


cJSON_AddItemToObject(Data_ReadytoSend, "Port Sensitivity", Sensitivity_data);
cJSON_AddNumberToObject(Sensitivity_data, "Port1", Settings.Port1_Sensitivity);
cJSON_AddNumberToObject(Sensitivity_data, "Port2", Settings.Port2_Sensitivity);
cJSON_AddNumberToObject(Sensitivity_data, "Port3", Settings.Port3_Sensitivity);
cJSON_AddNumberToObject(Sensitivity_data, "Port4", Settings.Port4_Sensitivity);
cJSON_AddNumberToObject(Sensitivity_data, "Port5", Settings.Port5_Sensitivity);
cJSON_AddNumberToObject(Sensitivity_data, "Port6", Settings.Port6_Sensitivity);
cJSON_AddNumberToObject(Sensitivity_data, "Port7", Settings.Port7_Sensitivity);
cJSON_AddNumberToObject(Sensitivity_data, "Port8", Settings.Port8_Sensitivity);

cJSON_AddStringToObject(Data_ReadytoSend, "Version", "V1.1");

data_buffer = cJSON_Print(Data_ReadytoSend);
Serial.println(data_buffer);
// writeFile(LittleFS, CONFIG_FILE, (uint8_t*)cJSON_Print(Data_ReadytoSend));
cJSON_free(data_buffer);
cJSON_Delete(Data_ReadytoSend);
cJSON_Delete(DR_Data);
cJSON_Delete(Sensitivity_data);
cJSON_Delete(Version_data);

}
正确的方法应该是使用cJSON_AddObjectToObject()来创建目录节点,这样子就不会有内存泄露的问题。
void update_info(void)
{
cJSON* Data_ReadytoSend=NULL;
cJSON* DR_Data=NULL;
cJSON* Sensitivity_data=NULL;
cJSON* Version_data=NULL;
char* data_buffer;

Data_ReadytoSend = cJSON_CreateObject();

DR_Data = cJSON_AddObjectToObject(Data_ReadytoSend, "Digital Potentiometer");
cJSON_AddNumberToObject(DR_Data, "CH1", Settings.Register1_RADC1);
cJSON_AddNumberToObject(DR_Data, "CH2", Settings.Register1_RADC2);
cJSON_AddNumberToObject(DR_Data, "CH3", Settings.Register2_RADC1);
cJSON_AddNumberToObject(DR_Data, "CH4", Settings.Register2_RADC2);
cJSON_AddNumberToObject(DR_Data, "CH5", Settings.Register3_RADC1);
cJSON_AddNumberToObject(DR_Data, "CH6", Settings.Register3_RADC2);
cJSON_AddNumberToObject(DR_Data, "CH7", Settings.Register4_RADC1);
cJSON_AddNumberToObject(DR_Data, "CH8", Settings.Register4_RADC2);

Sensitivity_data = cJSON_AddObjectToObject(Data_ReadytoSend, "Port Sensitivity");
cJSON_AddNumberToObject(Sensitivity_data, "Port1", Settings.Port1_Sensitivity);
cJSON_AddNumberToObject(Sensitivity_data, "Port2", Settings.Port2_Sensitivity);
cJSON_AddNumberToObject(Sensitivity_data, "Port3", Settings.Port3_Sensitivity);
cJSON_AddNumberToObject(Sensitivity_data, "Port4", Settings.Port4_Sensitivity);
cJSON_AddNumberToObject(Sensitivity_data, "Port5", Settings.Port5_Sensitivity);
cJSON_AddNumberToObject(Sensitivity_data, "Port6", Settings.Port6_Sensitivity);
cJSON_AddNumberToObject(Sensitivity_data, "Port7", Settings.Port7_Sensitivity);
cJSON_AddNumberToObject(Sensitivity_data, "Port8", Settings.Port8_Sensitivity);

Version_data = cJSON_AddObjectToObject(Data_ReadytoSend, "Version");
cJSON_AddStringToObject(Version_data, "Version", "V1.1");

data_buffer = cJSON_Print(Data_ReadytoSend);
Serial.println(data_buffer);
// writeFile(LittleFS, CONFIG_FILE, (uint8_t*)cJSON_Print(Data_ReadytoSend));
cJSON_free(data_buffer);
cJSON_Delete(Data_ReadytoSend);

}
`

posted @ 2025-06-19 14:27  Ary-ovo  阅读(58)  评论(0)    收藏  举报