Thread: glibc invalid pointer error

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30

    glibc invalid pointer error

    Hello,
    after Compilation with the gnu compiler ( g++-4.2 dbtest.cpp -Wall -pedantic -Wextra -o test -g ) and running the Program, i get the following error:

    Code:
    *** glibc detected *** ./test: free(): invalid pointer: 0x0804b04f ***
    ======= Backtrace: =========
    /lib/i686/cmov/libc.so.6[0xb7d998f5]
    /lib/i686/cmov/libc.so.6(cfree+0x90)[0xb7d9d360]
    /usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb7f67151]
    /usr/lib/libstdc++.so.6(_ZNSs4_Rep10_M_destroyERKSaIcE+0x1d)[0xb7f439ed]
    /usr/lib/libstdc++.so.6(_ZNSsD1Ev+0x51)[0xb7f453c1]
    ./test[0x80490e6]
    ./test[0x804915b]
    ./test(__gxx_personality_v0+0x4d4)[0x804905c]
    /lib/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7d44450]
    ./test(__gxx_personality_v0+0x49)[0x8048bd1]
    ======= Memory map: ========
    08048000-0804a000 r-xp 00000000 08:03 4726528    /home/ben/dateien/arbeit/coding/c++/projects/filedb/test
    0804a000-0804b000 rw-p 00001000 08:03 4726528    /home/ben/dateien/arbeit/coding/c++/projects/filedb/test
    0804b000-0806c000 rw-p 0804b000 00:00 0          [heap]
    b7c00000-b7c21000 rw-p b7c00000 00:00 0 
    b7c21000-b7d00000 ---p b7c21000 00:00 0 
    b7d2d000-b7d2e000 rw-p b7d2d000 00:00 0 
    b7d2e000-b7e76000 r-xp 00000000 08:03 147285     /lib/i686/cmov/libc-2.7.so
    b7e76000-b7e77000 r--p 00148000 08:03 147285     /lib/i686/cmov/libc-2.7.so
    b7e77000-b7e79000 rw-p 00149000 08:03 147285     /lib/i686/cmov/libc-2.7.so
    b7e79000-b7e7c000 rw-p b7e79000 00:00 0 
    b7e7c000-b7e88000 r-xp 00000000 08:03 784965     /lib/libgcc_s.so.1
    b7e88000-b7e89000 rw-p 0000b000 08:03 784965     /lib/libgcc_s.so.1
    b7e89000-b7e8a000 rw-p b7e89000 00:00 0 
    b7e8a000-b7ead000 r-xp 00000000 08:03 147290     /lib/i686/cmov/libm-2.7.so
    b7ead000-b7eaf000 rw-p 00023000 08:03 147290     /lib/i686/cmov/libm-2.7.so
    b7eaf000-b7f91000 r-xp 00000000 08:03 1880512    /usr/lib/libstdc++.so.6.0.10
    b7f91000-b7f94000 r--p 000e2000 08:03 1880512    /usr/lib/libstdc++.so.6.0.10
    b7f94000-b7f96000 rw-p 000e5000 08:03 1880512    /usr/lib/libstdc++.so.6.0.10
    b7f96000-b7f9c000 rw-p b7f96000 00:00 0 
    b7fb5000-b7fb8000 rw-p b7fb5000 00:00 0 
    b7fb8000-b7fd2000 r-xp 00000000 08:03 4137293    /lib/ld-2.7.so
    b7fd2000-b7fd4000 rw-p 00019000 08:03 4137293    /lib/ld-2.7.so
    bf95f000-bf974000 rw-p bffeb000 00:00 0          [stack]
    ffffe000-fffff000 r-xp 00000000 00:00 0          [vdso]
    Here is an extraction of the code, but please read first my comments at the end, before analysing it.

    filedb.h:
    Code:
    template <typename typ>
    class filedb{
      typedef const typ ctyp;
    
      private:
        char path_db[512];
        unsigned int number_of_records;
        typ* recarray;
    
        //shall be positiv if rec1 > rec2, negativ if rec1 < rec2
        //and 0 if rec1 = rec2
        int (*comp)( const typ& rec1, const typ& rec2 );
    
      public:
    
        //opens file with records and reads all records into memory
        filedb( std::string path_db,
    	    int (*comp)( const typ& rec1, const typ& rec2 ) );
        ~filedb();
    
        //inserts record into database (does not write change to file)
        void insert( const typ* record );
    };
    filedb.cpp:
    Code:
    //opens file with records and reads all records into memory
    template <typename typ>
    filedb<typ>::filedb( std::string p_db,
                         int (*comp)( const typ& rec1, const typ& rec2 ) ) :
                         comp(comp) 
    {
      strcpy( path_db, p_db.c_str() );
    
      number_of_records = 0;
      recarray = new typ[number_of_records];
    }
    
    //Destructor
    template <typename typ>
    filedb<typ>::~filedb(){
      delete [] recarray;
    }
    
    
    //inserts record into database (does not write change to file)
    template <typename typ>
    void filedb<typ>::insert( const typ* record ){
      typ* rectemp = recarray;
      recarray = new typ[number_of_records + 1];
      memcpy( recarray, rectemp, number_of_records*sizeof(typ) );
      delete [] rectemp;
    
      ++number_of_records;
    
      int i = number_of_records-1;
      for( ; i > 0 && comp(recarray[i], *record); --i ){
        memcpy( &recarray[i], &recarray[i-1], sizeof(typ) );
      }
    
      //insert record to the right place
      memcpy( &recarray[i], record, sizeof(typ) );                      //<------- the important line
    }
    dbtest.cpp
    Code:
    int main(){
      filedb<record> db( "./hello", comperator );
    
      record rec1;
      record rec2;
      record rec3;
      record rec4;
      record rec5;
      
      rec1.id = 5;
      rec1.str = "ich bins";
    
      rec2.id = 2;
      rec2.str = "du bists";
    
      rec3.id = 4;
      rec3.str = "wir sinds";
    
      db.insert( &rec1 );
      db.insert( &rec2 );
      db.insert( &rec3 );
    }

    Running gdb i discoverd, that the error occeurs when the programm frees the memory of recarray in the destructor. Furthermore, i discoverd, that after uncommenting "the important line" from filedb.cpp, the error ceased to exist. The memcpy function seems to be the evil element here, but i cannot figure out, why it leads to this error.
    Any help appreciated
    Ben

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    //inserts record into database (does not write change to file)
    template <typename typ>
    void filedb<typ>::insert( const typ* record ){
      typ* rectemp = recarray;
      recarray = new typ[number_of_records + 1];
      memcpy( recarray, rectemp, number_of_records*sizeof(typ) );
      delete [] rectemp;
    
      ++number_of_records;
    
      int i = number_of_records-1;
      for( ; i > 0 && comp(recarray[i], *record); --i ){
        memcpy( &recarray[i], &recarray[i-1], sizeof(typ) );
      }
    
      //insert record to the right place
      memcpy( &recarray[i], record, sizeof(typ) );                      //<------- the important line
    }
    Isn't 'i' equal to -1 when you end the loop?

    Edit: or at least, i = -1 if there are NO records in the list

    --
    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
    Dec 2007
    Location
    Germany
    Posts
    30
    Hi, thanks for the answer, but i don't think i will ever be -1, cause the decremention of i only is applied, after every loop, so it wont even be applied for i being 0 at the beginning.

    edit: it definitly wont get to -1, testet it with following program:
    Code:
    #include <iostream>
    int main(){
      int i = 0;
      for( ; i > 0; --i ) ;
      std::cout << i << std::endl;
    }
    Output: 0
    Last edited by benshi; 05-12-2008 at 09:36 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You should not be using memcpy() unless the type is restricted to a POD type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30
    Thank you a lot,
    that was my mistake and i'm sorry, that i left out the definition of record, which was a struct including a string.
    Ben

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM