Error: Double free or corruption (out)

This is a discussion on Error: Double free or corruption (out) within the C++ Programming forums, part of the General Programming Boards category; Hey all, I'm having some problems with the following code. I'm implementing a stop-n-wait file transfer protocol and this is ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    1

    Error: Double free or corruption (out)

    Hey all,

    I'm having some problems with the following code. I'm implementing a stop-n-wait file transfer protocol and this is the server/file receiver code. It takes in the file size and then one packet at a time, sending back an ACK each time. The code snags at the line indicated and gives the following error. Help?

    *** glibc detected *** ./serverUDP: double free or corruption (out): 0x0000000019fa7550 ***
    ======= Backtrace: =========
    /lib64/libc.so.6[0x2b03ed11ad7f]
    /lib64/libc.so.6(cfree+0x4b)[0x2b03ed11b1db]
    /lib64/libc.so.6(fclose+0x14b)[0x2b03ed1097cb]
    /opt/gcc-4.3.4/lib64/libstdc++.so.6(_ZNSt12__basic_fileIcE5closeEv+0x68 )[0x2b03ec9c8658]
    /opt/gcc-4.3.4/lib64/libstdc++.so.6(_ZNSt13basic_filebufIcSt11char_trai tsIcEE5closeEv+0xb7)[0x2b03ec976c47]
    /opt/gcc-4.3.4/lib64/libstdc++.so.6(_ZNSt14basic_ofstreamIcSt11char_tra itsIcEE5closeEv+0xd)[0x2b03ec97809d]
    ./serverUDP[0x4026bc]
    /lib64/libc.so.6(__libc_start_main+0xf4)[0x2b03ed0c7994]
    ./serverUDP(__gxx_personality_v0+0xc1)[0x401da9]
    ======= Memory map: ========
    00400000-00407000 r-xp 00000000 00:1b 947937269 /USERS/STUDENTS/F-K/K/KIESERJW/Documents/College/08 Spring 12/CS 462/ftpLab/serverUDP
    00607000-00608000 rw-p 00007000 00:1b 947937269 /USERS/STUDENTS/F-K/K/KIESERJW/Documents/College/08 Spring 12/CS 462/ftpLab/serverUDP
    19fa7000-19fc8000 rw-p 19fa7000 00:00 0 [heap]
    2b03ec6ec000-2b03ec708000 r-xp 00000000 08:01 520726 /lib64/ld-2.5.so
    2b03ec708000-2b03ec70b000 rw-p 2b03ec708000 00:00 0
    2b03ec908000-2b03ec909000 r--p 0001c000 08:01 520726 /lib64/ld-2.5.so
    2b03ec909000-2b03ec90a000 rw-p 0001d000 08:01 520726 /lib64/ld-2.5.so
    2b03ec90a000-2b03ec9f5000 r-xp 00000000 08:01 2701812 /opt/gcc-4.3.4/lib64/libstdc++.so.6.0.10
    2b03ec9f5000-2b03ecbf4000 ---p 000eb000 08:01 2701812 /opt/gcc-4.3.4/lib64/libstdc++.so.6.0.10
    2b03ecbf4000-2b03ecbfb000 r--p 000ea000 08:01 2701812 /opt/gcc-4.3.4/lib64/libstdc++.so.6.0.10
    2b03ecbfb000-2b03ecbfd000 rw-p 000f1000 08:01 2701812 /opt/gcc-4.3.4/lib64/libstdc++.so.6.0.10
    2b03ecbfd000-2b03ecc10000 rw-p 2b03ecbfd000 00:00 0
    2b03ecc10000-2b03ecc92000 r-xp 00000000 08:01 520892 /lib64/libm-2.5.so
    2b03ecc92000-2b03ece91000 ---p 00082000 08:01 520892 /lib64/libm-2.5.so
    2b03ece91000-2b03ece92000 r--p 00081000 08:01 520892 /lib64/libm-2.5.so
    2b03ece92000-2b03ece93000 rw-p 00082000 08:01 520892 /lib64/libm-2.5.so
    2b03ece93000-2b03ecea9000 r-xp 00000000 08:01 2701833 /opt/gcc-4.3.4/lib64/libgcc_s.so.1
    2b03ecea9000-2b03ed0a8000 ---p 00016000 08:01 2701833 /opt/gcc-4.3.4/lib64/libgcc_s.so.1
    2b03ed0a8000-2b03ed0a9000 rw-p 00015000 08:01 2701833 /opt/gcc-4.3.4/lib64/libgcc_s.so.1
    2b03ed0a9000-2b03ed0aa000 rw-p 2b03ed0a9000 00:00 0
    2b03ed0aa000-2b03ed1f7000 r-xp 00000000 08:01 520717 /lib64/libc-2.5.so
    2b03ed1f7000-2b03ed3f7000 ---p 0014d000 08:01 520717 /lib64/libc-2.5.so
    2b03ed3f7000-2b03ed3fb000 r--p 0014d000 08:01 520717 /lib64/libc-2.5.so
    2b03ed3fb000-2b03ed3fc000 rw-p 00151000 08:01 520717 /lib64/libc-2.5.so
    2b03ed3fc000-2b03ed403000 rw-p 2b03ed3fc000 00:00 0
    7fff1d6a0000-7fff1d6b5000 rw-p 7ffffffe9000 00:00 0 [stack]
    7fff1d7fd000-7fff1d800000 r-xp 7fff1d7fd000 00:00 0 [vdso]
    ffffffffff600000-ffffffffffe00000 ---p 00000000 00:00 0 [vsyscall]
    Abort



    Code:
    #include "PracticalSocket.h" // For UDPSocket and SocketException
    #include "CCRC32.H"
    #include "common.h"
    #include <iostream>          // For cout and cerr
    #include <cstdlib>           // For atoi()
    #include <fstream> 
    #include <cmath> 
    
    
    const int BUFMAX = 255;     // Longest string to echo
    
    int main(int argc, char *argv[]) {
    
        if (argc != 2) {                  // Test for correct number of parameters
            cerr << "Usage: " << argv[0] << " <Server Port>" << endl;
            exit(1);
        }
        cout << "Server running...\n";
        
        srand ( time(NULL) );
        
        CCRC32* MyCRC32 = new CCRC32();
        MyCRC32->Initialize();
        unsigned short echoServPort = atoi(argv[1]);     // First arg:  local port
    
        try {
            UDPSocket sock(echoServPort);                
            int recvMsgSize;
            int size;
            int maxSeq = 2;
            int seqNum = 0;
            int numBytes = ((int)((log(maxSeq)/log(2))/8))+1;
            int payloadSize = BUFMAX-numBytes-2;
            
            unsigned char * ack = new unsigned char [numBytes];
            unsigned char *memblock = new unsigned char [BUFMAX];
            
            string sourceAddress;             // Address of datagram source
            unsigned short sourcePort;        // Port of datagram source
            
            ofstream file;
            file.open("test1.out", ios::out|ios::binary);
            if (file.is_open()&& file.good())
            {    
                
                unsigned char* fileSizeArray = new unsigned char[8];
                int fileSize = 0;
                sock.recvFrom(fileSizeArray, 8, sourceAddress, sourcePort);
                printUInts(fileSizeArray,8,"fileSizeArray");
                for (int j = 0;j<8;j++){
                    fileSize |= fileSizeArray[j] << 8*j;
                }
                delete [] fileSizeArray;
                cout << "fileSize: " << fileSize<<endl;
                while (true){
                    memset(memblock,0,BUFMAX);
                    memset(ack,0,numBytes);
                    size = 0;
                    recvMsgSize = 0;
                    while  (size < BUFMAX) { 
                        // Block until receive message from a client
                        recvMsgSize = sock.recvFrom(memblock+size, BUFMAX, sourceAddress, sourcePort);
                        size += recvMsgSize;
    
                    }
                    int newSeqNum = 0;
                    memblock = unpacketize(memblock,BUFMAX,newSeqNum,numBytes,MyCRC32);
                    for (int i = 0;i<numBytes;i++){
                        ack[i] = newSeqNum >> 8*i;
                    }
                    if (newSeqNum == seqNum){
                        
                        if (fileSize <= payloadSize){
                            //cout << "last write\n";
                            printUInts(memblock+fileSize-16,16, "Last data");
                            cout << "file write output: "<< (file.write ((char*)memblock, fileSize))<<endl;
                            seqNum = maxSeq;
                            fileSize = 0;
                            cout << "COMPLETE TO LINE: " << __LINE__ <<endl;
                            if (file.is_open()){
                                file.close();   //errors out here
                            }                      
                            cout << "COMPLETE TO LINE: " << __LINE__ <<endl;
                        }else{
                            //printUInts(memblock,payloadSize, "data");
                            cout << "file write output: "<< (file.write((char*)memblock, payloadSize)) << endl;
                            fileSize-=payloadSize;
                        }
                        seqNum++;
                        if (seqNum==maxSeq){
                            seqNum = 0;
                        }
                        sock.sendTo(ack, numBytes, sourceAddress, sourcePort);
                    }
                }
                
                
                // NOT REACHED
            }else {cout << "Unable to open file";
            }
        } catch (SocketException &e) {
            cerr << e.what() << endl;
            exit(1);
        }
        
        
    
        return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    The first thing to do is to learn how to use the debugger.

    Namely, you do
    gcc -g prog.c
    gdb ./a.out

    When you run the program (in the debugger), it will catch all such exceptions, and then allow you to inspect the current program state to figure out what is wrong.

    Or you do this
    recvMsgSize = sock.recvFrom(memblock+size, BUFMAX, sourceAddress, sourcePort);
    size += recvMsgSize;
    You're lying about your buffer size - which you think is constant, but is in fact a decreasing amount.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Pretty sure that this line:

    Code:
                               file.close();   //errors out here
    cannot cause your problem.

    Note that cout is no good for debugging, because it is buffered. It is easily possible for this line to have been executed before the crash:

    Code:
                           cout << "COMPLETE TO LINE: " << __LINE__ <<endl;
    but the output never reached the screen; it was waiting in the stdout buffer.

    Use cerr (or better yet, Salem's suggestion).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help getting runtime double free or corruption error
    By wiggsfly in forum C Programming
    Replies: 9
    Last Post: 10-20-2008, 10:07 AM
  2. Replies: 3
    Last Post: 08-08-2008, 07:40 AM
  3. error: double free or corruption
    By dsc in forum C Programming
    Replies: 3
    Last Post: 04-03-2008, 09:26 AM
  4. Replies: 7
    Last Post: 11-26-2007, 12:11 PM
  5. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21