Getting *** glibc detected *** free(): invalid next size (normal):

Hi ,
When i am writing to a file continuously I am getting this error.

*** glibc detected *** ./MITE_Project2_Exe: free(): invalid next size (normal): 0x0000000028ba02f0 ***
======= Backtrace: =========
/lib64/libc.so.6[0x33c2c71cec]
/lib64/libc.so.6(cfree+0x8c)[0x33c2c7590c]
./MITE_Project2_Exe[0x40fe98]
./MITE_Project2_Exe[0x4124ce]
./MITE_Project2_Exe[0x4128de]
./MITE_Project2_Exe[0x414bf8]
./MITE_Project2_Exe[0x41560f]
/lib64/libc.so.6(__libc_start_main+0xf4)[0x33c2c1d974]
./MITE_Project2_Exe(__gxx_personality_v0+0xd1)[0x402a79]
======= Memory map: ========
00400000-0041f000 r-xp 00000000 00:17 10600310 /home/csgrad/heratyag/new/MITE_Project2_Exe
0061f000-00620000 rw-p 0001f000 00:17 10600310 /home/csgrad/heratyag/new/MITE_Project2_Exe
13433000-28baa000 rw-p 13433000 00:00 0 [heap]
33c2400000-33c241c000 r-xp 00000000 fd:00 63491 /lib64/ld-2.5.so
33c261b000-33c261c000 r--p 0001b000 fd:00 63491 /lib64/ld-2.5.so
33c261c000-33c261d000 rw-p 0001c000 fd:00 63491 /lib64/ld-2.5.so
33c2c00000-33c2d4c000 r-xp 00000000 fd:00 63494 /lib64/libc-2.5.so
33c2d4c000-33c2f4c000 ---p 0014c000 fd:00 63494 /lib64/libc-2.5.so
33c2f4c000-33c2f50000 r--p 0014c000 fd:00 63494 /lib64/libc-2.5.so
33c2f50000-33c2f51000 rw-p 00150000 fd:00 63494 /lib64/libc-2.5.so
33c2f51000-33c2f56000 rw-p 33c2f51000 00:00 0
33c3000000-33c3082000 r-xp 00000000 fd:00 63509 /lib64/libm-2.5.so
33c3082000-33c3281000 ---p 00082000 fd:00 63509 /lib64/libm-2.5.so
33c3281000-33c3282000 r--p 00081000 fd:00 63509 /lib64/libm-2.5.so
33c3282000-33c3283000 rw-p 00082000 fd:00 63509 /lib64/libm-2.5.so
33c8000000-33c800d000 r-xp 00000000 fd:00 63515 /lib64/libgcc_s-4.1.2-20080825.so.1
33c800d000-33c820d000 ---p 0000d000 fd:00 63515 /lib64/libgcc_s-4.1.2-20080825.so.1
33c820d000-33c820e000 rw-p 0000d000 fd:00 63515 /lib64/libgcc_s-4.1.2-20080825.so.1
33c8800000-33c88e6000 r-xp 00000000 fd:03 2687243 /usr/lib64/libstdc++.so.6.0.8
33c88e6000-33c8ae5000 ---p 000e6000 fd:03 2687243 /usr/lib64/libstdc++.so.6.0.8
33c8ae5000-33c8aeb000 r--p 000e5000 fd:03 2687243 /usr/lib64/libstdc++.so.6.0.8
33c8aeb000-33c8aee000 rw-p 000eb000 fd:03 2687243 /usr/lib64/libstdc++.so.6.0.8
33c8aee000-33c8b00000 rw-p 33c8aee000 00:00 0
2afe0367e000-2afe03680000 rw-p 2afe0367e000 00:00 0
2afe036a3000-2afe036a6000 rw-p 2afe036a3000 00:00 0
2afe03768000-2afe037e9000 rw-p 2afe03768000 00:00 0
2afe04000000-2afe04021000 rw-p 2afe04000000 00:00 0
2afe04021000-2afe08000000 ---p 2afe04021000 00:00 0
7fff5ca28000-7fff5ca3d000 rw-p 7ffffffea000 00:00 0 [stack]
ffffffffff600000-ffffffffffe00000 ---p 00000000 00:00 0 [vdso]
Abort



My program is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
#include <list>
#include "BinaryIO.h"

using namespace std;

BinaryIO :: BinaryIO(size_t sz)
{
        data = new char[sz];
}

BinaryIO :: BinaryIO()
{
        data = new char[150];
}

void BinaryIO :: writeBinFile(string filePath,list <unsigned long> writeData,ofstream& file)
{
        struct stat buffer;
        BinaryIO binWriteData;
        ifstream::pos_type size;
        int status;
        list <unsigned long> :: iterator itr;
        size = file.tellp();

        for(itr = writeData.begin();itr != writeData.end();itr++)
        {
                sprintf(data,"%lu",*itr);
                memset(data,' ',150);
                file.write(reinterpret_cast<char *>(&binWriteData),sizeof(BinaryIO));
        }
}


Please Help
Thanx in advance.
Last edited on
Passing in a list by value?

What is the value of sizeof(BinaryIO)?

Where is data declared? You write a number into it then clear it, did you mean to do that?
@kbw
Yes the list is passed by value

BinaryIO is a class which contains a member char* data . sizeof(BinaryIO) is the size of the data contained in a class.

basically i want to write it into a binary file which accepts arguments as char* but my entire data is stored in a list and that i have to write it into a file. The thing is i have to process a number of files collect data into the list and write it in a binary file. It works for few files but after that it gives me this error. that means for some file the memset value is larger than the one i have defined in the prog. But i am not able to figure out what could be a possible solution to this prob.

Thanks
Herat...
Contents of BinaryIO.h are as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <list>
#include <fstream>

using namespace std;

class BinaryIO
{
        private:
                char* data;

        public:
                void writeBinFile(string,list<unsigned long>,ofstream&);
                BinaryIO(size_t);
                BinaryIO();
};
Don't pass the list by value.

Have you printed the value of sizeof(BinaryIO)? What value do you think it is? I'm sure you'll be surprised?

You could help youself by checking the values of things before asking for help. Debugging is a part programming.
At first glance there seems to be a number of potential pitfalls.

First, your class allocates memory in the constructor but does not free the memory in the destructor since you don't provide a destructor. This problem is actually saving you a bit, because BinaryIO does not have the correct copy semantics; you need to write a copy constructor and assignment operator.

Second, one constructor allocates an array 150 elements wide, the other takes a parameter to know how big to
make the array. But writeBinFile() seems to just assume the array is 150 elements wide (see memset). So
if you ever use the parameterized constructor and give it a value less than 150, you will stomp the heap (which
could lead to the original problem).

Thirdly, line 31 is pointless since line 32 overwrites the memory just written. And on the subject of line 31...
sprintf() is extremely unsafe. It writes however many characters need to be written, without regard to the
actual length of the destination buffer (since it doesn't know the length). Since "data" is on the heap, you
would again corrupt the heap if you wrote past the end of the buffer. You are better off using snprintf(),
which does allow you to prevent the buffer overrun.

(Fourthly, don't use namespaces in header files.)
Topic archived. No new replies allowed.