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