Question

Is this memory leak caused by fopen()? I used fclose() to close the FILE* pointer but this problem is still there.

==32379== Memcheck, a memory error detector
==32379== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==32379== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==32379== Command: ./speller /home/cs50/pset6/texts/austinpowers.txt
==32379== 

==32379== 
==32379== HEAP SUMMARY:
==32379==     in use at exit: 352 bytes in 1 blocks
==32379==   total heap usage: 510,177 allocs, 510,176 frees, 57,140,478 bytes allocated
==32379== 
==32379== 352 bytes in 1 blocks are still reachable in loss record 1 of 1
==32379==    at 0x4006AB1: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==32379==    by 0x4D9F895B: __fopen_internal (iofopen.c:73)
==32379==    by 0x4D9F8A3A: fopen@@GLIBC_2.1 (iofopen.c:103)
==32379==    by 0x8049061: load (dictionary.c:50)
==32379==    by 0x8048759: main (speller.c:46)
==32379== 
==32379== LEAK SUMMARY:
==32379==    definitely lost: 0 bytes in 0 blocks
==32379==    indirectly lost: 0 bytes in 0 blocks
==32379==      possibly lost: 0 bytes in 0 blocks
==32379==    still reachable: 352 bytes in 1 blocks
==32379==         suppressed: 0 bytes in 0 blocks
==32379== 
==32379== For counts of detected and suppressed errors, rerun with: -v
==32379== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Here is the load function(The fopen() call is 'dictionary.c:50'):

bool load(const char* dictionary)
{
    // Open the dictionary file.
    FILE* file;
    file = fopen(dictionary,"r");
    // If the file exists...
    if(file)
    { 
        // If the load function is called twice, this condition makes sure that we don't do it twice.
        if(start==NULL)
        {
            start = malloc(sizeof(trie));
            for(int i =0; i < 28; i++)
            {
                start->array[i]=NULL;
            }
        }
        else
        {
            return true;
        }
        char c = '\0';
        char* word = malloc(LENGTH+1);
        while((c = fgetc(file))!=EOF)
        {  
            int index = 0;            
            for(;c!='\n';index++)
            {
                word[index] = c;
                c=fgetc(file);
            }
            // End the word.
            word[index++] = '\0';             
            // now add it to the trie.
            add(word);
            //Increment the number of words.
            numOfWords++;
        }
        //free the word and close the file.
        free(word);
        fclose(file);        
        return true;       
    }
    return false;
}
Was it helpful?

Solution

If start (a global variable?) is != NULL, you will leave the function without closing the file.

OTHER TIPS

Make sure that you're truly using the files you think you are. If you're passing a null input file into your valgrind command you might have unexpected behavior

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top