0

Below is the part of script and the same is invoked from main function. If i comment out the below function, everything works fine without any memory leak issue, whereas by including this function, the process ends up with memory leak and eventually it stops over a span of 2 hrs.

void passRFIDInfo(int antenna_id, int peakRssi, char *fast_id, int reader_mac_flag, int data_accumulated_flag){

  if (reader_mac_flag == 0){
    struct ifreq s;

      int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);


    strcpy(s.ifr_name, "eth0");
    if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
    int i;

    for (i = 0; i < 6; ++i){
      unsigned char data  =  s.ifr_addr.sa_data[i];
      sprintf(reader_mac+(i*2), "%02x", data);
    }
    reader_mac[12] = '\0';
    }
  }

  char uniqueID[40];



  char *antennaID = (char*)malloc(2);
  snprintf(antennaID, sizeof(antennaID), "%d", antenna_id);


  strcpy(uniqueID, reader_mac);

  strcat(uniqueID, ".");
  strcat(uniqueID, antennaID);
  strcat(uniqueID, ".");
  strcat(uniqueID, fast_id);



  int json_size = json_object_size(root);

  if (data_accumulated_flag==1 && json_size!=0) 
  {

    sendMQTT(rfid_json_dict);

    free(rfid_json_dict);      
    json_decref(root);

    json_t *root = json_object();  
    char *rfid_json_dict;
  }

  json_object_set_new( root, uniqueID, json_integer(peakRssi));
  rfid_json_dict = json_dumps(root, 0);

  printf("rfid_json_dict ::%s\n",rfid_json_dict);
  printf("\n");

}

Before the program executes, the available memory in an embedded linux platform is,

>show system cpu
Status='0,Success'
TotalMemory='62304256'
FreeMemory='18621122'
CPUUtilization='3'

As the execution continues for 2 hrs and so, the FreeMemory keeps going down and eventually at the end of 3 hrs, the process killed automatically. How to resolve such kind of issue?

Mahamutha M
  • 1,235
  • 1
  • 8
  • 24
  • 1
    `char *antennaID = (char*)malloc(2);` allocates 2 bytes but you never `free` it. It's not much, but after 2 hours that will mount up. – Weather Vane Jun 18 '20 at 18:37
  • It's quite strange code though: `char *rfid_json_dict;` was defined *after* you first use it. How does it compile? Is `rfid_json_dict` "shadowing" a global variable of the same name? – Weather Vane Jun 18 '20 at 18:39
  • Also fun: `char *antennaID = (char*)malloc(2); snprintf(antennaID, sizeof(antennaID), "%d", antenna_id);` spot the error(s). – EOF Jun 18 '20 at 20:23
  • 1
    @EOF I supposed it's a single digit value, but the 7 lines which build `uniqueID` can be replaced with a single `snprintf` anyway. – Weather Vane Jun 18 '20 at 20:45
  • @WeatherVane No cookie for you. It's the `sizeof(pointer)`. – EOF Jun 18 '20 at 20:49
  • 1
    @EOF uh-oh the cookie crumbled. – Weather Vane Jun 18 '20 at 20:49

1 Answers1

0

Just as it was pointed out in the comments, you're missing a call to free() on antennaID. So you malloc() memory for it, but then it's never freed. So it just keeps piling up.

The general rule is that for every call to malloc(), there should be a call to free(). At least, that's how I learned, and that's what makes the most sense to me. As soon as you're done using a variable, you should free() it. Briefly going through that code, it doesn't look like antennaID ever leaves the function? So you should be good to just free it before the function returns.