2017-07-24 50 views
0

我正在開發一個tizen網絡應用程序並實施一些後臺操作,我正在使用本機服務。該服務在一段時間內執行得很好,之後,當使用free分配一些用malloc分配的內存時,它會經常崩潰。當內存釋放時,Tizen本機服務偶爾崩潰

登錄貓如下:

07-13 19:44:54.529+0900 W/AUL  (2463): app_signal.c: aul_send_app_launch_request_signal(521) > aul_send_app_launch_request_signal app(org.example.emedicalbtleservice) pid(7630) type(svcapp) bg(0) 
07-13 19:44:54.529+0900 W/AUL  (7629): launch.c: app_request_to_launchpad(298) > request cmd(0) result(7630) 
07-13 19:44:54.539+0900 W/STARTER (2654): pkg-monitor.c: _app_mgr_status_cb(395) > [_app_mgr_status_cb:395] Launch request [7630] 
07-13 19:44:54.569+0900 W/AUL_AMD (2463): amd_request.c: __request_handler(669) > __request_handler: 14 
07-13 19:44:54.579+0900 W/AUL_AMD (2463): amd_request.c: __send_result_to_client(91) > __send_result_to_client, pid: 7630 
07-13 19:44:54.579+0900 W/AUL_AMD (2463): amd_request.c: __request_handler(669) > __request_handler: 14 
07-13 19:44:54.599+0900 W/AUL_AMD (2463): amd_request.c: __send_result_to_client(91) > __send_result_to_client, pid: 7630 
07-13 19:44:54.599+0900 W/AUL_AMD (2463): amd_status.c: __socket_monitor_cb(1277) > (7630) was created 
07-13 19:44:54.599+0900 W/AUL_AMD (2463): amd_request.c: __request_handler(669) > __request_handler: 12 
07-13 19:44:54.599+0900 W/AUL_AMD (2463): amd_request.c: __request_handler(669) > __request_handler: 12 
07-13 19:44:54.729+0900 E/PKGMGR_SERVER(7542): pkgmgr-server.c: exit_server(1619) > exit_server Start [backend_status=1, queue_status=1] 
07-13 19:44:54.729+0900 E/PKGMGR_SERVER(7542): pkgmgr-server.c: main(2295) > package manager server terminated. 
07-13 19:44:54.799+0900 I/emedicalbtleservice(7630): /opt/usr/media 
07-13 19:44:54.799+0900 I/emedicalbtleservice(7630): /opt/usr/media/eMedicalBP.txt 
07-13 19:44:54.989+0900 W/AUL  (7638): daemon-manager-release-agent.c: main(12) > release agent : [2:/org.example.emedicalbtleservice] 
07-13 19:44:54.989+0900 W/AUL_AMD (2463): amd_request.c: __request_handler(669) > __request_handler: 23 
07-13 19:44:54.989+0900 W/AUL_AMD (2463): amd_request.c: __send_result_to_client(91) > __send_result_to_client, pid: 0 
07-13 19:44:54.989+0900 W/AUL_AMD (2463): amd_request.c: __request_handler(1032) > pkg_status: installed, dead pid: 7630 
07-13 19:44:54.989+0900 W/AUL_AMD (2463): amd_request.c: __send_app_termination_signal(528) > send dead signal done 
07-13 19:44:55.009+0900 I/AUL_AMD (2463): amd_main.c: __app_dead_handler(262) > __app_dead_handler, pid: 7630 
07-13 19:44:55.009+0900 W/AUL  (2463): app_signal.c: aul_send_app_terminated_signal(799) > aul_send_app_terminated_signal pid(7630) 
07-13 19:44:55.009+0900 W/CRASH_MANAGER(7637): worker.c: worker_job(1205) > 0607630656d65149994269 

代碼片段:

char* read_file(const char* filepath) 
{ 
    FILE *fp = fopen(filepath, "r"); 
    if (fp == NULL) 
    { 
     dlog_print(DLOG_ERROR, LOG_TAG, "Cannot open file"); 
     return NULL; 
    } 
    fseek(fp, 0, SEEK_END); 
    int bufsize = ftell(fp); 
    rewind(fp); 
    if (bufsize < 1) 
    { 
     dlog_print(DLOG_ERROR, LOG_TAG, "Cannot open file"); 
     return NULL; 
    } 
    char *buf = malloc(sizeof(char) * (bufsize)); 
    memset(buf, '\0', sizeof(buf)); 
    char str[200]; 
    while(fgets(str, 200, fp) != NULL) 
    { 
     sprintf(buf + strlen(buf), "%s", str); 
    } 
    fclose(fp); 
    return buf; 
} 

void get_password(char *filePath, int *password, bool *has_password) 
{ 
    char *fileContent = read_file(filePath); //charater pointer pointed to data read from file and memory allocated with malloc 
    if (fileContent == NULL) 
    { 
     *has_password = false; 
     dlog_print(DLOG_ERROR, LOG_TAG, "Do not have password"); 
     return; 
    } 
    else 
    { 
     cJSON *root = cJSON_Parse(fileContent); 
     free(fileContent); 
     if (root != NULL && cJSON_IsObject(root)) 
     { 
      cJSON *passwordArray = cJSON_DetachItemFromObjectCaseSensitive(root, "passwordArray"); 
      cJSON_Delete(root); 
      root = NULL; 
      if (cJSON_IsArray(passwordArray)) 
      { 
       for (int i = 0; i < cJSON_GetArraySize(passwordArray); ++i) 
       { 
        password[i] = cJSON_GetArrayItem(passwordArray, i)->valueint; 
       } 
       dlog_print(DLOG_INFO, LOG_TAG, "Has password"); 
       *has_password = true; 
      } 
      else 
      { 
       dlog_print(DLOG_ERROR, LOG_TAG, "Password is not array"); 
       *has_password = false; 
      } 
      cJSON_Delete(passwordArray); 
     } 
     else 
     { 
      dlog_print(DLOG_ERROR, LOG_TAG, "Content cannot be parsed"); 
      *has_password = false; 
     } 

     if (root != NULL) 
     { 
      cJSON_Delete(root); 
     } 
    } 
} 

我使用CJSON庫來解析存儲在文件中的一些內容JSON。該服務經常死機它執行free(fileContent);cJSON_Delete(root);

回答

2

read_file後,您基於文件大小buf分配空間,但你不考慮通過sprintf寫的空終結,讓你有一個緩衝區溢出,可造成堆腐敗。 (您也可以使用fgets直接讀入緩衝區。)

+0

謝謝你,這似乎解決了這個問題。 –