简体   繁体   中英

C - Can't get rid of memory leaks

I did the program which reads words from one file, sorts them and inserts into list, then I print the list into new file. Words have to be sorted alphabetically. I used strcmp() function to compare them and I created function MakeLowerCase(). It "converts" string to lowercase string. I know that program make a lot of operations but I have no idea how could I optimize it. Anyway, I have to get rid of memory leaks, but I don't know where it leaks.

I've got such report from Dr Memory (only part of it):


error #5: LEAK 2 direct bytes 0x01373d60-0x01373d62 + 0 indirect bytes
# 0 replace_malloc                     [d:\drmemory_package\common\alloc_replace.c:2577]
# 1 msvcrt.dll!_strdup   
# 2 .text      
# 3 __mingw_glob
# 4 _setargv   
# 5 .text      
# 6 ntdll.dll!__RtlUserThreadStart

Error #6: LEAK 11 direct bytes 0x01373db0-0x01373dbb + 0 indirect bytes
# 0 replace_malloc               [d:\drmemory_package\common\alloc_replace.c:2577]
# 1 MakeItTxt  
# 2 main       

Error #7: LEAK 6 direct bytes 0x01374f10-0x01374f16 + 0 indirect bytes
# 0 replace_malloc                    [d:\drmemory_package\common\alloc_replace.c:2577]
# 1 MakeLowerCase
# 2 sortedInsert
# 3 InsertWordsToStruct
# 4 main       

Error #8: LEAK 6 direct bytes 0x01374f38-0x01374f3e + 0 indirect bytes
# 0 replace_malloc                    [d:\drmemory_package\common\alloc_replace.c:2577]
# 1 MakeLowerCase
# 2 sortedInsert
# 3 InsertWordsToStruct
# 4 main       

Error #9: LEAK 4 direct bytes 0x013750a0-0x013750a4 + 0 indirect bytes
# 0 replace_malloc                    [d:\drmemory_package\common\alloc_replace.c:2577]
# 1 MakeLowerCase
# 2 sortedInsert
# 3 InsertWordsToStruct
# 4 main       

Error #10: LEAK 6 direct bytes 0x013750c8-0x013750ce + 0 indirect bytes
# 0 replace_malloc                    [d:\drmemory_package\common\alloc_replace.c:2577]
# 1 MakeLowerCase
# 2 sortedInsert
# 3 InsertWordsToStruct
# 4 main       

Reached maximum leak report limit (-report_leak_max). No further leaks will be reported.

===========================================================================
FINAL SUMMARY:

DUPLICATE ERROR COUNTS:
    Error #   1:      8
    Error #   2:      8
    Error #   4:      3
    Error #   7:    137
    Error #   8:    137
    Error #   9:   4855
    Error #  10:   4854

SUPPRESSIONS USED:

ERRORS FOUND:
      0 unique,     0 total unaddressable access(es)
      3 unique,    17 total uninitialized access(es)
      0 unique,     0 total invalid heap argument(s)
      0 unique,     0 total GDI usage error(s)
      0 unique,     0 total handle leak(s)
      0 unique,     0 total warning(s)
      7 unique,  9988 total,  68700 byte(s) of leak(s)
      0 unique,     0 total,      0 byte(s) of possible leak(s)
ERRORS IGNORED:
     10 unique,    12 total,    421 byte(s) of still-reachable allocation(s)

I can share with you part of my code (the functions from report):


char* MakeLowerCase(char* word)
{
  char* lower = (char *)malloc(sizeof(char)*strlen(word)+1);
  strcpy(lower, word);
  int i = 0;

  for(i = 0; i < strlen(lower); i++){
    lower[i] = tolower(lower[i]);
  }
  return lower;
}

Word* newItem(char* word)
{
    /* allocate node */
    Word* new_item = (Word *)malloc(sizeof(Word));
    int word_length = strlen(word);
    new_item->word = (char *)malloc((word_length+1)*sizeof(char));

    strcpy(new_item->word, word);
    new_item->pNext = NULL;

    return new_item;
}

void sortedInsert(Word** pH, Word* new_node)
{
    Word* current;
    /* Special case for the head end */
    if (*pH == NULL || strcmp(MakeLowerCase((*pH)->word), MakeLowerCase(new_node->word)) == 1)
    {
        new_node->pNext = *pH;
        *pH = new_node;
    }
    else
    {
        /* Locate the node before the point of insertion */
        current = *pH;
        while (current->pNext!=NULL &&
               strcmp(MakeLowerCase(current->pNext->word), MakeLowerCase(new_node->word)) == -1)
        {
            current = current->pNext;
        }
        new_node->pNext = current->pNext;
        current->pNext = new_node;
    }
}

void InsertWordsToStruct(Word** pH, FILE* filename){
  char single_line[16384]; /* longest posible line in my code */

  int number_of_words = 0;
  int counter = 0;

  while(fgets(single_line, 16384, filename))
  {
    char* single_word = strtok(single_line, " \t\n\0"); /* cut one word from line */
    while(single_word != NULL)
    {
      if(IsLegitWord(single_word) == true) /* function return true if word is really word */
      {
        Word* new_node = newItem(single_word);
        sortedInsert(pH, new_node);
      }
      single_word = strtok(NULL, " \t\n\0");
    }
  }
}

I really cannot find the leaks. I am new in C, sorry. Of course, at the end of the file I use in main:


RemoveWordList(&listname);

Function:


void RemoveWordList(Word** pH){
  Word* current = *pH;
  while (current != NULL){
      Word* next = current->pNext;
      free(current->word);
      free(current);
      current = next;
  }
  free(current);
  *pH = NULL;
}

^but I think the problem isn't here.^ Have you any ideas? Could you help me?

At the very least, this line

if (*pH == NULL || strcmp(MakeLowerCase((*pH)->word), MakeLowerCase(new_node->word)) == 1)

is continuously leaking memory since MakeLowerCase allocates memory that is never freed.

Consider using something like this

....
char *x1, *x2;
....
// add appropriate NULL checks here before proceeding
x1 = MakeLowerCase((*pH)->word);
x2 = MakeLowerCase(new_node->word);
if (*pH == NULL || strcmp(x1, x2) == 1)
....
free(x1);
free(x2);

It is also a good practice to check whether you actually got the memory you attempted to allocate.

After suggestion of DNT-user I remade the function sortedInsert(). Now it look like this:


void sortedInsert(Word** pH, Word* new_node)
{
    Word* current;
    /* Special case for the head end */
    char* temp_word1 = MakeLowerCase((*pH)->word);
    char* temp_word2 = MakeLowerCase(new_node->word);
    if (*pH == NULL || strcmp(temp_word1, temp_word2 ) == 1)
    {
        new_node->pNext = *pH;
        *pH = new_node;
    }
    else
    {
        /* Locate the node before the point of insertion */
        current = *pH;
        while (current->pNext!=NULL &&
               strcmp(MakeLowerCase(current->pNext->word), MakeLowerCase(new_node->word)) == -1)
        {
            current = current->pNext;
        }
        new_node->pNext = current->pNext;
        current->pNext = new_node;
    }
    free(temp_word1);
    free(temp_word2);
}

There is a fewer memory leaks, but now my program doens't make output file (probably something went wrong).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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