简体   繁体   中英

Memory can't be freed in C

The following wifi_json() method keeps increasing memory when i call it in a while loop to test. Is it because the structs used in the method not getting released or i dont use the free method to free up the memory properly?

#include "MiCO.h" 
#include "json_c/json.h"
#define os_json_log(M, ...) custom_log("JSON", M, ##__VA_ARGS__)
void wifi_json()
{
  os_json_log("%s","start");
  struct json_object *recv_json_object_device_wifi=NULL;
  recv_json_object_device_wifi=json_object_new_object();      

  struct json_object *recv_json_object_array_wifi=NULL;
  recv_json_object_array_wifi=json_object_new_array(); //wifi array

  struct json_object *recv_json_object_wifi=NULL; //recv wifi
  recv_json_object_wifi=json_object_new_object();

  struct json_object *wifi_object=NULL; //wifi
  wifi_object=json_object_new_object();

  //wifi
 json_object_object_add(wifi_object, "connected", json_object_new_string("1"));    
 json_object_object_add(wifi_object, "strength", json_object_new_string("60")); 
 json_object_object_add(wifi_object, "ip", json_object_new_string("60"));
 json_object_object_add(wifi_object, "mac", json_object_new_string("60"));
 json_object_object_add(wifi_object, "ssid", json_object_new_string("60"));
 json_object_object_add(recv_json_object_wifi,"WIFI",wifi_object);

 json_object_array_add(recv_json_object_array_wifi,recv_json_object_wifi);
 json_object_array_add(recv_json_object_array_wifi,recv_json_object_wifi);
 json_object_array_add(recv_json_object_array_wifi,recv_json_object_wifi);

 json_object_object_add(recv_json_object_device_wifi,"WIFI_Info",recv_json_object_array_wifi);
 os_json_log("%s",json_object_to_json_string(recv_json_object_device_wifi));

 free(recv_json_object_device_wifi);
 recv_json_object_device_wifi=NULL;

 free(recv_json_object_array_wifi);
 recv_json_object_array_wifi=NULL;

 json_object_put(recv_json_object_wifi);
 json_object_put(wifi_object);


 json_object_put(recv_json_object_array_wifi);
 json_object_put(recv_json_object_device_wifi);
}
int application_start( void )
{  
 while(1)
 {
    wifi_json(); 
    os_json_log("Free memory has %d bytes", MicoGetMemoryInfo()->free_memory) ;
   }
  }

You need to free the memory created by these two as well:

struct json_object *recv_json_object_wifi=NULL; //recv wifi
recv_json_object_wifi=json_object_new_object();

struct json_object *wifi_object=NULL; //wifi
wifi_object=json_object_new_object();

I think your problem is in the os_json_log("%s",json_object_to_json_string(recv_json_object_device_wifi)); .

You should assign a pointer to it and free it later.

const char *str = json_object_to_json_string(recv_json_object_device_wifi);
os_json_log("%s", str);
free(str);

Further, just doing a

json_object_put(recv_json_object_device_wifi);

Is enough , as that is the root object to which all others are added. No need to call json_object_put on any of its children.

Please try to use this to release memory of the json object "json_object_object_del()"

json_object_object_del(recv_json_object_wifi); recv_json_object_wifi=NULL; json_object_object_del (wifi_object); wifi_object=NULL;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM