我在函数的开头创建一个char数组,在wnd中我希望它被删除。
到目前为止,这是我的代码:
void GetPiece(int size)
{
//std::cout << "Downloading File Index: " + currFileIndex << " More: " + pieceSize - size;
char * newBuffer = new char[size];
bitRequest->RecvBySize(newBuffer, size - 4, 5);
newBuffer += 8;
File file("work_torrent.mp3");
file.WriteToFile(newBuffer, size - 4);
file.Close();
pieceSize -= size -4;
std::cout << "Downloading File Index: " << currFileIndex << " More: " << pieceSize << std::endl;
if (pieceSize <= 0)
{
currFileIndex++;
pieceSize = pieces.at(currFileIndex).fileSize;
}
offset += size - 4;
if (pieceSize > 16384)
{
if (1048576 - offset < 16384)
{
requestSize = 1048576 - offset;
if (requestSize == 0)
{
offset = 0;
currPieceIndex++;
if (pieceSize > 16384)
{
requestSize = 16384;
}
else {
requestSize = pieceSize;
}
}
}
else {
requestSize = 16384;
}
}
else {
requestSize = pieceSize;
}
bitRequest->RequestPiece(currPieceIndex, offset, requestSize);
delete[] newBuffer; // Try to delete it
}
当它到达删除char数组的行时,它会给出一个错误:
当我没有删除这个char数组时,代码在调用函数后会抛出一个分配错误。
修改
有人知道它为什么会发生?