简体   繁体   English

C - 如何解决 memory 泄漏?

[英]C - How to resolve memory leaks?

Below is the part of script and the same is invoked from main function.下面是脚本的一部分,同样是从主 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. 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,在程序执行之前,嵌入式linux平台中可用的memory是,

>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.随着执行持续 2 小时等,FreeMemory 不断下降,最终在 3 小时结束时,进程自动终止。 How to resolve such kind of issue?如何解决此类问题?

Just as it was pointed out in the comments, you're missing a call to free() on antennaID .正如评论中指出的那样,您错过了对天线 ID 的free()antennaID So you malloc() memory for it, but then it's never freed.所以你malloc() memory 为它,但它从未被释放。 So it just keeps piling up.所以它只会不断堆积。

The general rule is that for every call to malloc() , there should be a call to free() .一般规则是,每次调用malloc()时,都应该调用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.一旦你使用完一个变量,你应该free()它。 Briefly going through that code, it doesn't look like antennaID ever leaves the function?简要浏览该代码,它看起来好像天线 ID 不会离开antennaID So you should be good to just free it before the function returns.所以你应该在 function 返回之前释放它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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