我有一个看起来类似于这样的数据包结构:
struct packet {
sockaddr_in m_Recv;
int m_iPacketSize;
unsigned char* m_ucpPacket;
}
这是我使用此结构的代码的选择:
packet tOutPacket;
tOutPacket.m_Recv = tInPacket.m_Recv;
tOutPacket.m_iPacketSize = 256;
tOutPacket.m_ucpPacket = new unsigned char[256];
// Creating a uuid (not important how it's done, the pointer has 16 bytes)
unsigned char* uuid = NetworkHandlerN::ConnectionData::createUUID(hardware_hash);
NetworkHandlerN::PacketHandler::addUUIDToPacket(tOutPacket.m_ucpPacket, uuid);
delete[] uuid;
UDPsend(tOutPacket.m_Recv, tOutPacket.m_ucpPacket, tOutPacket.m_iPacketSize);
/* Delete pointer */
delete[] tOutPacket.m_ucpPacket; // This is causing an error
这是我的 UDPsend 方法:
bool NBsocket::UDPsend(sockaddr_in& AddrSento, const unsigned char* cucpBuffer, int iSize)
{
if(sendto(m_iSocket, reinterpret_cast<const char*>(cucpBuffer), iSize, 0, (sockaddr*) &AddrSento, sizeof(AddrSento)) == -1) {
m_iErrorCode = 6;
return false;
}
return true;
}
这是 addUUIDToPacket 方法:
static void addUUIDToPacket(unsigned char* ucpPacket, unsigned char* ucpChar)
{
for(int i = 0; i < ciUUIDSize; i++) {
ucpPacket[ciHeaderSize+i] = ucpChar[i];
}
}
首先,我以为我在tOutPacket.m_ucpPacket
某处删除了指针,但我检查了它。如果我像这样(在 之前delete[]
)对存储在里面的数据进行测试:
if(tOutPacket.m_ucpPacket[tOutPacket.m_iPacketSize-1] == 0x08)
std::cout << "0x08";
它工作得很好(是的,值应该是 0x08)
这是我的调用堆栈输出:
ntdll.dll!77dcfadc() Unknown
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
ntdll.dll!77db4f92() Unknown
ntdll.dll!77d926fc() Unknown
ntdll.dll!77dd0b37() Unknown
ntdll.dll!77d8a967() Unknown
> msvcp110.dll!std::_Fputc<char>(char _Byte, _iobuf * _File) Line 83 C++
msvcp110.dll!std::basic_filebuf<char,std::char_traits<char> >::overflow(int _Meta) Line 383 C++
msvcp110.dll!std::basic_streambuf<char,std::char_traits<char> >::xsputn(const char * _Ptr, __int64 _Count) Line 406 C++
我已经测试了很多,但无法弄清楚出了什么问题。
希望可以有人帮帮我。如果需要,我可以添加该/* add data to packet */
部分,但这将是更多代码。
编辑
发现错误。我向这个方法传递了一个字节:
static void addToPacket(unsigned char* ucpPacket, int iInt, int iPos)
{
ucpPacket[iPos+0] = iInt & 0xFF;
ucpPacket[iPos+1] = iInt >> 8 & 0xFF;
ucpPacket[iPos+2] = iInt >> 8 & 0xFF;
ucpPacket[iPos+3] = iInt >> 8 & 0xFF;
}
这导致了所有的麻烦......太多的方法重载:)。一定是失去了概述。