Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-20-2009, 12:33 AM   #1
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: =========
======= 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/
33c261b000-33c261c000 r--p 0001b000 fd:00 63491 /lib64/
33c261c000-33c261d000 rw-p 0001c000 fd:00 63491 /lib64/
33c2c00000-33c2d4c000 r-xp 00000000 fd:00 63494 /lib64/
33c2d4c000-33c2f4c000 ---p 0014c000 fd:00 63494 /lib64/
33c2f4c000-33c2f50000 r--p 0014c000 fd:00 63494 /lib64/
33c2f50000-33c2f51000 rw-p 00150000 fd:00 63494 /lib64/
33c2f51000-33c2f56000 rw-p 33c2f51000 00:00 0
33c3000000-33c3082000 r-xp 00000000 fd:00 63509 /lib64/
33c3082000-33c3281000 ---p 00082000 fd:00 63509 /lib64/
33c3281000-33c3282000 r--p 00081000 fd:00 63509 /lib64/
33c3282000-33c3283000 rw-p 00082000 fd:00 63509 /lib64/
33c8000000-33c800d000 r-xp 00000000 fd:00 63515 /lib64/
33c800d000-33c820d000 ---p 0000d000 fd:00 63515 /lib64/
33c820d000-33c820e000 rw-p 0000d000 fd:00 63515 /lib64/
33c8800000-33c88e6000 r-xp 00000000 fd:03 2687243 /usr/lib64/
33c88e6000-33c8ae5000 ---p 000e6000 fd:03 2687243 /usr/lib64/
33c8ae5000-33c8aeb000 r--p 000e5000 fd:03 2687243 /usr/lib64/
33c8aeb000-33c8aee000 rw-p 000eb000 fd:03 2687243 /usr/lib64/
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]

My program is

#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++)
                memset(data,' ',150);
                file.write(reinterpret_cast<char *>(&binWriteData),sizeof(BinaryIO));
Old 10-20-2009, 06:06 AM   #2
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Stretch (Fluxbox WM)
Posts: 1,389
Blog Entries: 52

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

Original Poster
Rep: Reputation: 0
#include <iostream>
#include <string>
#include <list>
#include <fstream>

using namespace std;

class BinaryIO
                char* data;

                void writeBinFile(string,list<unsigned long>,ofstream&);
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
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Stretch (Fluxbox WM)
Posts: 1,389
Blog Entries: 52

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
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.


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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:42 AM.

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