c++ - std::bad_alloc 运行时异常
问题描述
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
解决方案
一种可能的解释可能是 bufferSize 过大。如果可能,尝试调试代码,或打印出 bufferSize 以查看它。
推荐阅读
- python - 尝试将 3D 数据绘制为彩色网格时如何解决“没有足够的值来解包”?
- c++ - 在头文件中定义结构实例
- javascript - 从 ajax 响应数据制作堆积柱形图
- linux - 为什么一个文件描述符上的flock(2)会受到同一进程中另一个文件描述符的干扰
- sql - Powershell 连接到 SQL Server 的几个实例
- sql - 在 Vertica 中使用 UNION ALL 运算符运行很长的 SQL 查询时出错
- mysql - 在 Mysql 触发器中将行复制到列
- remote-access - 会话登录后 Ubuntu 20.04 x11vnc 空白(非黑色)屏幕
- javascript - React Material UI DataGrid:无法读取未定义的属性'useRef'
- google-apps-script - 如何在 Google Sheet Query 中使用 select * all 添加额外的自定义列