Przeglądaj źródła

ntp和mac2个地方内存泄露

guohui 2 lat temu
rodzic
commit
39e73d093c
6 zmienionych plików z 28 dodań i 32 usunięć
  1. 0 3
      pro/src/app.c
  2. 0 3
      pro/src/appweb_handle.c
  3. 4 6
      pro/src/common.c
  4. 3 3
      pro/src/common.h
  5. 1 4
      pro/src/sqlite_handle.c
  6. 20 13
      pro/src/sys.c

+ 0 - 3
pro/src/app.c

@@ -1944,9 +1944,6 @@ int _global_device_manage_init(GlobalDeviceManager* _globalDeviceManager)
    #endif
     //创建传感器采集控制线程
 
-    _globalNTPManager.ntpaddress = malloc(128);
-    _globalNTPManager.ntpName = malloc(128);
-    _globalNTPManager.time = malloc(22);
     ret = dev_get_ntp_info(_globalDeviceManager->db,  &_globalNTPManager);
     if (ret == -1)
     {

+ 0 - 3
pro/src/appweb_handle.c

@@ -3586,13 +3586,10 @@ static void serviceManagement(void *conn)
     else if(strcmp(_serviceManageRequestInfo.type,"NtpSave")==0)
     {
         _globalNTPManager.mode = atoi(_serviceManageRequestInfo._ntp_info.mode);
-       _globalNTPManager.ntpaddress= malloc(64);
         strcpy(_globalNTPManager.ntpaddress, _serviceManageRequestInfo._ntp_info.ntpaddress);
-        _globalNTPManager.ntpName= malloc(64);
         strcpy(_globalNTPManager.ntpName, _serviceManageRequestInfo._ntp_info.ntpName);
         _globalNTPManager.port = atoi(_serviceManageRequestInfo._ntp_info.port);
         _globalNTPManager.product_id = __globalDeviceManage._globalDevInfo.product_id;
-         _globalNTPManager.time= malloc(64);
         strcpy(_globalNTPManager.time, _serviceManageRequestInfo._ntp_info.time);
         const char* key = "GMT/UTC";
         char* p = strstr(_serviceManageRequestInfo._ntp_info.tzone, key);

+ 4 - 6
pro/src/common.c

@@ -200,14 +200,13 @@ char* get_mac_address(const char *iface_name)
     unsigned char *mac; 
     static char mac_str[32];
 
-
     snprintf(mac_str, sizeof(mac_str), "%02d:%02d:%02d:%02d:%02d:%02d",  
-             0, 0,0, 0, 0, 0);  
+             0, 0, 0, 0, 0, 0);  
 
     // 创建一个socket  
     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {  
         perror("socket error");  
-        return strdup(mac_str);
+        return mac_str;
     }  
   
     // 设置接口名称  
@@ -217,7 +216,7 @@ char* get_mac_address(const char *iface_name)
     if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) {  
         perror("ioctl error");  
         close(fd);  
-        return strdup(mac_str);   
+        return mac_str;   
     }  
       // ifr.ifr_hwaddr.sa_data是一个包含硬件地址的字节数组  
     mac = (unsigned char *)ifr.ifr_hwaddr.sa_data; 
@@ -228,8 +227,7 @@ char* get_mac_address(const char *iface_name)
     // 关闭socket  
     close(fd);  
   
-    // 成功返回0  
-    return strdup(mac_str);  
+    return mac_str;  
 }
 
 

+ 3 - 3
pro/src/common.h

@@ -347,10 +347,10 @@ typedef struct
 {
     int                 product_id;
     int                 mode;
-    char*               ntpName;
-    char*               ntpaddress;
+    char                ntpName[64];
+    char                ntpaddress[64];
     int                 port;
-    char*               time;
+    char                time[64];
     int                 weekday;
     int                 tzone;
 }NTPManager;

+ 1 - 4
pro/src/sqlite_handle.c

@@ -2883,13 +2883,10 @@ int dev_get_ntp_info(sqlite3 *db,NTPManager* _globalNTPInfo)
 
     //获取ID
     _globalNTPInfo->product_id = atoi(pResult[rowIndex*ncolumn+0]); 
-    _globalNTPInfo->mode = atoi(pResult[rowIndex*ncolumn+1]); 
-   _globalNTPInfo->ntpName= malloc(64);
+    _globalNTPInfo->mode = atoi(pResult[rowIndex*ncolumn+1]);
     strcpy(_globalNTPInfo->ntpName,pResult[rowIndex*ncolumn+2]);
-    _globalNTPInfo->ntpaddress= malloc(64);
     strcpy(_globalNTPInfo->ntpaddress ,pResult[rowIndex*ncolumn+3]);
     _globalNTPInfo->port = atoi(pResult[rowIndex*ncolumn+4]); 
-     _globalNTPInfo->time= malloc(64);
     strcpy(_globalNTPInfo->time,pResult[rowIndex*ncolumn+5]);
     _globalNTPInfo->weekday = atoi(pResult[rowIndex*ncolumn+6]); 
     _globalNTPInfo->tzone = atoi(pResult[rowIndex*ncolumn+7]); 

+ 20 - 13
pro/src/sys.c

@@ -18,7 +18,7 @@
 #include "common.h"
 #include "sqlite_handle.h"
 
-#if 0
+#if 1
 #define LOGE  log_e
 #define LOGW  log_w
 #define LOGD  log_d
@@ -947,8 +947,9 @@ int sys_get_disk_ava_MB(char *path)
 
 int sys_get_mem_ava_MB(void)
 {
-    int r,value=0;
-    char buff[32];
+    int r;
+    long v1=0,v2;
+    char buf[32];
     struct sysinfo info;
 
 #if 0
@@ -964,14 +965,21 @@ int sys_get_mem_ava_MB(void)
         return -1;
     }
 
-    fscanf(fp, "MemTotal: %s kB\n", buff);
-    fscanf(fp, "MemFree: %s kB\n", buff);
-    fscanf(fp, "MemAvailable: %s kB\n", buff);
-    value = atol(buff)/1024;
+    fscanf(fp, "MemTotal: %s kB\n", buf);
+    
+    fscanf(fp, "MemFree: %s kB\n", buf);
+    v1 = atol(buf);
+    
+    fscanf(fp, "MemAvailable: %s kB\n", buf);
+    v2 = atol(buf);
+    
     fclose(fp);
+    LOGD("___MemFree: %ldkB, MemAvailable: %ldkB\n", v1, v2);
+    
+    v2 /= 1024;
 #endif
 
-    return value;
+    return v2;
 }
 
 
@@ -1144,7 +1152,7 @@ quit:
 //#define ROM_DEBUG
 
 #ifdef RAM_DEBUG
-    #define RAM_THRD        177
+    #define RAM_THRD        50
     #define RAM_PERIOD      1
 #else
     #define RAM_THRD        50
@@ -1152,7 +1160,7 @@ quit:
 #endif
 
 #ifdef ROM_DEBUG
-    #define ROM_THRD        500
+    #define ROM_THRD        50
     #define ROM_PERIOD      1
 #else
     #define ROM_THRD        50
@@ -1167,16 +1175,15 @@ static void mem_check(void)
     mem_cnt++;
     if(mem_cnt%RAM_PERIOD==0) {
         avaMB = sys_get_mem_ava_MB();
-        printf("___ available mem:  %dMB, threshold: %dMB\n", avaMB, RAM_THRD);
         if(avaMB<RAM_THRD) {
-            printf("___ free mem less than %dMB, reboot now ...\n", RAM_THRD);
+            LOGE("___ free mem less than %dMB, reboot now ...\n", RAM_THRD);
             sys_reboot();
         }
     }
 
     if(mem_cnt%ROM_PERIOD==0) {
         avaMB = sys_get_disk_ava_MB("/");
-        printf("___ available rom: %dMB, threshold: %dMB\n", avaMB, ROM_THRD);
+        LOGD("___ available rom: %dMB, threshold: %dMB\n", avaMB, ROM_THRD);
         if(avaMB<ROM_THRD) {
             flag = 1;
         }