我正在使用VS2012。我正在写一个简单的网络游戏。发送/接收功能正常工作。我不明白为什么程序仍然在同一时刻抛出相同的异常 - 当waitForOthers()函数完成时。我切换了很多断点,看起来程序在这个函数中被破坏之后崩溃了BEFORE在更新乐趣中的waitForOthersMove()之后。
Expcetion: “StatkiKlient.exe中0x64D1CCC8(msvcp110d.dll)的第一次机会异常:0xC0000005:访问冲突读取位置0x004B6CF4。
如果存在此异常的处理程序,则可以安全地继续该程序。“
vs2012指向的xutility代码:
// MEMBER FUNCTIONS FOR _Container_base12
inline void _Container_base12::_Orphan_all()
{ // orphan all iterators
#if _ITERATOR_DEBUG_LEVEL == 2
if (_Myproxy != 0)
{ // proxy allocated, drain it
_Lockit _Lock(_LOCK_DEBUG);
for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter;
HERE -> *_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter) <- HERE
(*_Pnext)->_Myproxy = 0;
_Myproxy->_Myfirstiter = 0;
}
#endif /* _ITERATOR_DEBUG_LEVEL == 2 */
}
以下是代码:
“的main.cpp”:
#include "Client.h"
Client * client;
void clientLoop();
int main()
{
client = new Client();
clientLoop();
return 0;
}
void clientLoop()
{
while(true)
{
client->update();
}
}
“Client.cpp”:
#include "Client.h"
Client::Client()
{
initialized = false;
me = new Gamer();
cnet = new ClientNetwork();
}
void Client::update()
{
if(!initialized)
waitForStart(); //here crashes
waitForOthersMove();
}
void Client::waitForOthersMove()
{
Packet packet;
char data[2000];
int iResult = 0;
std::cout<<"Oczekiwanie na przeciwnikow...\n\n";
while(true)
{
iResult = cnet->receveData(data, sizeof(Packet));
if(iResult < 0)
continue;
packet.deserialize(data);
if(packet.packet_type = YOUR_MOVE)
{
//action
break;
}
}
}
void Client::consoleInit() //only for tests
{
Gamer set_up;
Packet packet;
char data[2000];
set_up.nickname = "... " + me->id;
set_up.ships[0].dir = 2;
set_up.ships[0].size = 2;
set_up.ships[0].x = 0;
set_up.ships[0].y = 0;
set_up.ships[0].dir = 1;
set_up.ships[0].size = 2;
set_up.ships[0].x = 10;
set_up.ships[0].y = 15;
set_up.ships[0].dir = 2;
set_up.ships[0].size = 2;
set_up.ships[0].x = 0;
set_up.ships[0].y = 0;
set_up.ships[0].dir = 2;
set_up.ships[0].size = 2;
set_up.ships[0].x = 0;
set_up.ships[0].y = 0;
set_up.ships[0].dir = 2;
set_up.ships[0].size = 2;
set_up.ships[0].x = 0;
set_up.ships[0].y = 0;
set_up.ships[0].dir = 2;
set_up.ships[0].size = 2;
set_up.ships[0].x = 0;
set_up.ships[0].y = 0;
packet.player = set_up;
packet.serialize(data);
cnet->sendMessage(data, sizeof(Packet));
}
void Client::gameInit()
{
Packet packet;
char packet_data[2000];
initialized = true;
std::cout<<"My id: "<<me->id<<"\n";
packet.packet_type = GAME_INIT_R;
packet.serialize(packet_data);
cnet->sendMessage(packet_data, sizeof(Packet));
consoleInit();
std::cout<<"wyslano game_init_r\n";
}
void Client::waitForStart()
{
Packet packet;
int messageLength = 0;
printf("\nPołączono z serwerem. Oczekiwanie na pozostałych graczy...\n");
cnet->testConnection();
while(true)
{
messageLength = cnet->receveData(dataBuff, sizeof(Packet));
if(messageLength < 0)
continue;
packet.deserialize(dataBuff);
if(packet.packet_type = GAME_INIT)
{
me->id = packet.player.id;
gameInit();
printf("Initialized...");
break;
}
}
} - after this parenthesis exception is thorw(i toggled breakpoint here and after this in update function)