c++memory-managementmemory-leaksdynamic-memory-allocationbad-alloc

std::bad_alloc runtime exception


I get the std::bad_aloc exception on server side, when the for loop reaches the second round. Here is the code:

void UDP_ProcessClient (int socketFileDescriptor, void *transmitBuffer, void *receiveBuffer, size_t transmitBufferSize, struct sockaddr *clientAddress, socklen_t clientAddressLength, char *workDir, size_t fileNumber)
{
    size_t i;
    size_t bufferSize = 0;
    string resultName, imageName;

    for (i = 0; i < 6; i++)
    {
        /// Start communicating (read/write)
        /// Prepare buffers for communication
        cout << "[Server]: Receiving data from client..." << endl;
        UDP_ReceiveData(socketFileDescriptor, (uint32_t *) &bufferSize, sizeof(bufferSize), MSG_WAITALL, clientAddress, &clientAddressLength);      /// Get required data buffer size
        //cout << "[Server]: PHPDHPHPHFPSADHFI" << endl;
        receiveBuffer = new uint8_t[bufferSize];                                                                                                    /// Allocate memory
        clog << "Receiving file of size " << bufferSize << endl;
        UDP_ReceiveData(socketFileDescriptor, (int8_t *) receiveBuffer, bufferSize, MSG_WAITALL, clientAddress, &clientAddressLength);              /// Read data
        //cout << "SDFDSFDSFFADSFASDFSDVLALALL " << bufferSize << endl;

        cout << '\a';
        cout << "[Server]: Data received successful" << endl;

        /// Save image.bmp file
        resultName = workDir;
        imageName = "image_" + to_string(fileNumber) + ".jai";
        resultName += imageName;
        createImageFromBuffer((uint8_t *) receiveBuffer, bufferSize, resultName);

        /// Clear memory
        bufferSize = 0;
        delete[] (uint8_t *) receiveBuffer;
    }
}

After each allocation of memory new [] at the beginning of round I don't forget to free delete [] it at the end of the end of each round. All files that are received are the same size. I can show the client code, but I think it works correctly.

Client side code:

    for (i = 0; i < 6; i++)
    {
        /// Do useful work (payload)
        /// Prepare files
        imageFileFullName = pathToImageFiles + imageFileName + std::to_string(i) + imageFileFormat;
        std::clog << "Opening file..." << std::endl;
        imageFile = new std::fstream;
        imageFile->open(imageFileFullName, std::ios::in | std::ios::binary | std::ios::ate);
        //checkFileOpened(imageFile);
        if (!(imageFile->is_open()))
        {
            std::cerr << "Can't open file " << imageFileFullName << ": ";
            perror("");
            exit(EXIT_FAILURE);
        }
        imageFileSize = imageFile->tellg();
        imageFile->seekg(0, std::ios::beg);
        /// Prepare transmit buffer
        transmitBuffer = new uint8_t[imageFileSize];
        std::cout << "Reading file..." << std::endl;
        imageFile->read((char *) transmitBuffer, imageFileSize);
        imageFile->close();

        delete imageFile;                                           /// Delete used file

        /// Send/Receive Data to/from Server
        std::cout << "Sending file..." << std::endl;
        UDP_SendData(socketFileDescriptor, &imageFileSize, sizeof(imageFileSize), MSG_CONFIRM, (struct sockaddr *) addressIterator_Pointer->ai_addr, addressIterator_Pointer->ai_addrlen);
        UDP_SendBigPacket(socketFileDescriptor, (uint8_t *) transmitBuffer, imageFileSize, MSG_CONFIRM, (struct sockaddr *) addressIterator_Pointer->ai_addr, addressIterator_Pointer->ai_addrlen);

        delete[] transmitBuffer;                                    /// Free memory
    }
...

A runtime exception on server side:

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted

Solution

  • One of possible explanations could be that bufferSize is unreasonably big. Try to debug the code if possible, or print out the bufferSize to see it.