std :: bad_alloc运行时异常

问题描述 投票:0回答:1

for循环到达第二轮时,我在服务器端收到std :: bad_aloc异常。这是代码:

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;
    }
}

在回合开始时分配每个内存new []之后,我不会忘记在每个回合结束时释放delete []它。接收到的所有文件均大小相同。我可以显示客户端代码,但我认为它可以正常工作。

客户端代码:

    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
    }
...

服务器端的运行时异常:

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted
c++ memory-management memory-leaks dynamic-memory-allocation bad-alloc
1个回答
1
投票

可能的解释之一是bufferSize过大。如果可能,请尝试调试代码,或打印出bufferSize以查看它。

© www.soinside.com 2019 - 2024. All rights reserved.