LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-20-2009, 12:33 AM   #1
herat.acharya
LQ Newbie
 
Registered: Oct 2009
Posts: 2

Rep: Reputation: 0
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


Code:
#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));
        }
}
 
Old 10-20-2009, 06:06 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Can you also attach the contents of BinaryIO.h?
 
Old 10-20-2009, 02:15 PM   #3
herat.acharya
LQ Newbie
 
Registered: Oct 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Code:
#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();
};
BinaryIO is a class which contains a member char* data . sizeof(BinaryIO) is the size of the data contained in a class.
This error is comin in writeBinFile

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.
 
Old 10-23-2009, 03:50 AM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
There are a few issues here.

1. When you allocate space using a 'new []', you should have a corresponding 'free []' (in your example, it would be in the BinaryIO destructor). Otherwise it will leak memory, eventually using up the whole lot.

2. I'm not sure why, but a second BinaryIO is instantiated on the stack inside the writeBinFile member function (it copies the writeData to data, but writes to the file from the local BinaryIO object).

3. Worse, it takes a pointer to this object and accesses it. What the file write is accessing is the data pointer, not the data buffer that was allocated (and so going off the end of the object altogether).

4. It is not good practice to have public data members in classes (one of the key ideas of object oriented languages is to hide data).

The code that you have posted is not itself going to cause the memory corruption you are seeing (since the only memory writes are the sprintf and memset). But the fact that you are treating the object as a buffer (instead of dereferencing its data pointer), have a fixed buffer length by default, and have external access to the data pointer probably means that some code is writing off into no-mans land. Either that, or you are running out of memory...

I'm not meaning to be overly critical, just pointing out some logical issues in the code. My suggestion would be to use the stream i/o to write the output file, rather than using your own buffer class (it isn't clear why you need the class at all). C++ streams are much cleaner than the C printf style conversions, once you get used to them.

Last edited by neonsignal; 10-23-2009 at 03:53 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error running a C program, glibc detected *** free(): invalid next size (normal) mesh2005 Programming 3 11-08-2006 01:52 PM
*** glibc detected *** free(): invalid next size (normal): 0x0000000000503e70 *** vbreddy Programming 2 04-10-2006 06:27 PM
*** glibc detected *** free(): invalid next size (normal): 0x0804c050 *** water&sky Linux - General 2 03-03-2006 12:25 PM
*** glibc detected *** free(): invalid next size (normal): 0x0804c050 *** water&sky Linux - Software 1 03-02-2006 08:23 AM


All times are GMT -5. The time now is 08:36 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration