2013-05-01 100 views
0

我有記憶喪失的問題,我解決不了:內存丟失Valgrind的

==19660== 14,583 (1,764 direct, 12,819 indirect) bytes in 49 blocks are definitely lost in loss record 27 of 27 
==19660== at 0x4023F50: malloc (vg_replace_malloc.c:236) 
==19660== by 0x80489F5: AllocateFct (function.c:27) 
==19660== by 0x804BB51: InsertFct (reader.c:417) 
==19660== by 0x804BFFB: InsertShape (reader.c:591) 
==19660== by 0x804AEED: main (main.c:103) 

生成由以下特點:

function AllocateFct(char* model) 
{ 

    function fct = (function) malloc(sizeof(struct _function_)); 

    if (fct == NULL) 
     return NULL;  
    if (model==NULL) 
     fct->model = NULL; 
    else 
    { 
     fct->model = malloc(sizeof(char) * (strlen(model) + 1)); 
     fct->model[0] = '\0'; 
     if (fct->model==NULL){ 
      fprintf(stderr,"Not enough memory!\n"); 
      exit(1); 
     } 
     strcpy(fct->model, model); 
    } 
    fct->detail = NULL; 
    fct->brief = NULL; 
    fct->bug = NULL; 
    fct->f = NULL; 
    fct->Parameter = NULL; 
    fct->ret = NULL; 
    fct->def = NULL; 
    fct->next = NULL; 

    return fct; 
} 

============= ================

void InsertFct(function* fct, content c) 
{ 
    if (type > 0) 
    { 
     type = c.code; 
     (*fct)->def = (char*) malloc(sizeof(char) * (strlen(c.message) + 1)); 
    (*fct)->def[0]='\0'; 
    if ((*fct)->def==NULL){ 
     fprintf(stderr,"Not enough memory!\n"); 
     exit(1); 
    } 
      strcpy((*fct)->def, c.message); 

      return; 
    } 
    switch (c.code) 
    { 
     case FN: 
       type = 0; 
       *fct = AllocateFct(c.message); 
       break; 
     case PARAM: 
       type = 0; 
       AddParameterFunction(*fct, c.message); 
       break; 
     case BRIEF: 
       type = 0; 
       AddBriefFunction(*fct, c.message); 
       break; 
     case DETAILS: 
       type = 0; 
       AddDetailFunction(*fct, c.message); 
       break; 
     case RETURN: 
       AddRetourFunction(*fct, c.message); 
       break; 
     case BUG: 
       type = 0; 
       AddBugFunction(*fct, c.message); 
       break; 
     default: 
       type = c.code; 
      AddDefautFunction(*fct,c.message); 
       break; 
    } 
} 

===========================

int InsertShape(list heading, list source, shape* finalShape, shape* tmp) 
{ 
    fileRead* file = NULL;   
    shape s = NULL; 
    shape insert = AllocateShape(NULL); 
    function fct = AllocateFct(NULL); 
    function fctlist = NULL; 
    int i = 0; 
    int j = 0; 
    int pile = 0; 
    int n = 0; 
    int tag = 0; 
    list headerlist = NULL; 
    content c;     
    char chaine; 

    if (heading != NULL)     
    { 
      char* recover = (char*) GetNameFile(heading->name); 
      s = AllocateShape(recover); 
     free(recover); 
     recover=NULL; 
    } 
    else if (source != NULL) 
    { 
      char* recover = (char*) GetNameFile(source->name); 
      s = AllocateShape(recover); 
     free(recover); 
     recover=NULL; 
    } 
    if (heading != NULL) 
    { 
      file = OpenFileRead(heading->name); 
      n = CheckInclude(file->f, &headerlist); 
      if (n == 0) 
      n = 1; 

      fseek(file->f, -n, SEEK_CUR); 
      do 
      { 
       i = 0; 
       j = RecoverComments(file, &c, &tag); 
       InsertFct(&fct, c); 
       if(c.message!=NULL){ 
       free(c.message); 
       c.message=NULL; 
      } 

       if (type > 0) 
       AddToShape(insert, fct); 

       if (j == -5 && i == 0) 
       { 
        type = 0; 
        do 
        { 
         if (fscanf(file->f, "%c", &chaine) == EOF) 
         { 
           i = 0; 
           break; 
         } 
        } while (chaine == ' ' || chaine == '\n'); 
        if (chaine == '/') 
        { 
         i = 1; 
         j = 0; 
         fseek(file->f, -1, SEEK_CUR); 
        } 
       } 
       if (i == 0 && (j == -5)) 
       { 
        if (type == 0) 
        { 
         pile++; 
         fctlist = AddFunction(fctlist, fct); 
        } 
        else 
        type = 0; 

       } 
      } while (j != -3); 
      FreefileRead(file); 
    } 
    i = 0; 
    j = 0; 
    type = 0; 
    if (source != NULL) 
    { 
      file = OpenFileRead(source->name); 
      n = CheckInclude(file->f, &headerlist); 
      if (n == 0) 
      n = 1; 

      fseek(file->f, -n, SEEK_CUR); 
      do 
      { 
       i = 0; 
       j = RecoverComments(file, &c, &tag); 
       InsertFct(&fct, c); 

       if(c.message!=NULL){ 
       free(c.message); 
       c.message=NULL; 
      } 

       if (type > 0) 
       AddToShape(insert, fct); 

       if (j == -5 && i == 0) 
       { 
        type = 0; 
        do 
        { 
         if (fscanf(file->f, "%c", &chaine) == EOF) 
         { 
           i = 0; 
           break; 
         } 
        } while (chaine == ' ' || chaine == '\n'); 
        if (chaine == '/') 
        { 
         i = 1; 
         j = 0; 
         fseek(file->f, -1, SEEK_CUR); 
        } 
       } 
       if (i == 0 && (j == -5)) 
       { 
        if (type == 0) 
        fctlist = TourAndChange(fctlist, fct); 

        else 
        type = 0; 

       } 
      } while (j != -3); 
      FreefileRead(file); 
    } 
    s->fctList = AddFunction(s->fctList, fctlist); 
    s->headerList = headerlist; 
    *finalShape = CreateListShape(*finalShape, s); 
    *tmp = CreateListShape(*tmp, insert); 

    return 1; 
} 

===================== 的部分關注主:

InsertShape(tmpheading, tmproot, &s, &s2); 
    if (heading == NULL){ 
     tmpheading = NULL; 
    } 
     if (root == NULL){ 
     tmproot = NULL; 
    } 
} 

預先感謝您爲您的答覆。

+1

內存活躍度或可達性是程序的全局屬性。這個錯誤可以在你的代碼中的其他地方...... – 2013-05-01 15:55:21

+1

這不會幫助我們解決這個問題。 – 2013-05-01 15:56:42

+0

另外,對於其他讀者。看起來'function'是'struct *'類型的typedef。所以'InsertFcn'實際上是一個'struct **'類型的對象。 – 2013-05-01 15:57:35

回答

2

那麼你什麼時候有空?它看起來不像你有一個免費的代碼! (除了「freefileread」,但我們不知道是什麼一樣,它似乎並不專爲功能!)

1

事情,我注意到:

fct->model = malloc(sizeof(char) * (strlen(model) + 1)); 
    fct->model[0] = '\0'; 
    if (fct->model==NULL){ 
     fprintf(stderr,"Not enough memory!\n"); 
     exit(1); 
    } 
    strcpy(fct->model, model); 

這裏fct->model[0] = '\0';可提領一空指針。它應該被刪除。此外,this block of code could be replaced with a call to strdup.


void InsertFct(function* fct, content c) 
{ 
    if (type > 0) 

...什麼是type?某種全球?不好的設計如果是這樣

接下來的代碼可能會再次被strdup的調用所取代。


InsertShape(tmpheading, tmproot, &s, &s2); 
if (heading == NULL){ 
    tmpheading = NULL; 
} 
    if (root == NULL){ 
    tmproot = NULL; 
} 

你爲什麼設置指針爲NULL不首先解放出來?順便說一下,free(NULL)什麼都不做,所以你不必做一些像if(ptr) free(ptr); ...只需撥打free(ptr);