freeing a file pointer without upsetting Valgrind

This is a discussion on freeing a file pointer without upsetting Valgrind within the C Programming forums, part of the General Programming Boards category; I thought I was finally clear of bugs when Valgrind printed a huge list of errors about a file I ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    129

    freeing a file pointer without upsetting Valgrind

    I thought I was finally clear of bugs when Valgrind printed a huge list of errors about a file I open and later free. They all come from "(below main)"; here are the first and last:
    Code:
    ==32022== Invalid read of size 4
    ==32022==    at 0x40B11AD: (within /lib/tls/i686/cmov/libc-2.8.90.so)
    ==32022==    by 0x40B1E7F: (within /lib/tls/i686/cmov/libc-2.8.90.so)
    ==32022==    by 0x4072DE9: exit (in /lib/tls/i686/cmov/libc-2.8.90.so)
    ==32022==    by 0x405A68C: (below main) (in /lib/tls/i686/cmov/libc-2.8.90.so)
    ==32022==  Address 0x41a49f8 is 104 bytes inside a block of size 352 free'd
    ==32022==    at 0x4024E5A: free (vg_replace_malloc.c:323)
    ==32022==    by 0x804A3F6: outline_free (outline.c:324)
    ==32022==    by 0x8049369: main (main.c:234)
    
    ==32022== Invalid read of size 4
    ==32022==    at 0x416BD4A: (within /lib/tls/i686/cmov/libc-2.8.90.so)
    ==32022==    by 0x416BDD9: (within /lib/tls/i686/cmov/libc-2.8.90.so)
    ==32022==    by 0x401F422: _vgnU_freeres (vg_preloaded.c:60)
    ==32022==    by 0x40E12B3: _Exit (in /lib/tls/i686/cmov/libc-2.8.90.so)
    ==32022==    by 0x405A68C: (below main) (in /lib/tls/i686/cmov/libc-2.8.90.so)
    ==32022==  Address 0x41a49ec is 92 bytes inside a block of size 352 free'd
    ==32022==    at 0x4024E5A: free (vg_replace_malloc.c:323)
    ==32022==    by 0x804A3F6: outline_free (outline.c:324)
    ==32022==    by 0x8049369: main (main.c:234)
    And here are the involved functions:
    Code:
    struct outline *outline_load(char *title){
        struct outline *this;
        if(title == (char *)NULL || strcmp(title, "") == 0)
            return (struct outline *)NULL;
        
        this = outline_create(title);
        char *path = malloc(strlen(outline_home) + 7 + strlen(title) + 1),
             *pattern = "%s" SLASH "files" SLASH "%s";
        CHECK(path);
        
        sprintf(path, pattern, outline_home, title);
        this->old = file_exists(path);
        if(this->old == 0)
            COMMAND("echo >", path); 
        this->file = fopen(path, "w+");
        
        ...
        
        free(path);
        return this;
    }
    
    void outline_free(void *o){
        struct outline *this = (struct outline *)o;
        outline_item_free(this->root);
        free(this->file);
        free(this);
    }
    I don't recall doing anything significant just before the errors appeared... Any ideas?

    Thanks!
    Chris

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You should NOT free what you get back from fopen() - it is a pointer, but there is absolutely no certainty that it is dynamically allocated - and if it is, fclose() should free it as well - so you freeing this->file is not meaningful.

    It is also very likely that fopen() is designed something along this way [certainly NOT the code you would find in the C library, but the concept is likely]:
    Code:
    FILE files[10000];
    int nFiles; 
    
    FILE *fopen(...)
    {
       ....  a load of code to actually open a file. 
       nFiles++;
       return &files[nFiles];
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    Oh... That works; thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21