我正在嘗試使用地圖系統來存儲和更新聊天服務器的數據。該應用程序是多線程的並使用鎖定系統來防止多線程訪問數據。C++映射迭代和堆棧損壞
問題是這樣的:當一個客戶端被單獨從地圖中刪除時,就沒關係。但是,當我嘗試調用多個關閉時,它會在內存中留下一些內容。如果我在地圖上的任何一個地方調用:: clear(),它會導致一個調試斷言錯誤,或者「迭代器不兼容」或類似。代碼將第一次運行(使用80+控制檯作爲測試連接進行測試),但由於它將塊放在後面,將不再工作。我已經嘗試過研究方法,並且我已經編寫了系統來停止代碼執行,直到每個進程完成。我非常感謝迄今爲止所提供的幫助,並附上了相關的代碼片段。
//portion of server code that handles shutting down
DWORD WINAPI runserver(void *params) {
runserverPARAMS *p = (runserverPARAMS*)params;
/*Server stuff*/
serverquit = 0;
//client based cleanup
vector<int> tokill;
map<int,int>::iterator it = clientsockets.begin();
while(it != clientsockets.end()) {
tokill.push_back(it->first);
++it;
}
for(;;) {
for each (int x in tokill) {
clientquit[x] = 1;
while(clientoffline[x] != 1) {
//haulting execution until thread has terminated
}
destoryclient(x);
}
}
//client thread based cleanup complete.
return 0;
}
//clientioprelim
DWORD WINAPI clientioprelim(void* params) {
CLIENTthreadparams *inparams = (CLIENTthreadparams *)params;
/*Socket stuff*/
for(;;) {
/**/
}
else {
if(clientquit[inparams->clientid] == 1)
break;
}
}
clientoffline[inparams->clientid] = 1;
return 0;
}
int LOCKED; //exported as extern via libraries.h so it's visible to other source files
void destoryclient(int clientid) {
for(;;) {
if(LOCKED == 0) {
LOCKED = 1;
shutdown(clientsockets[clientid], 2);
closesocket(clientsockets[clientid]);
if((clientsockets.count(clientid) != 0) && (clientsockets.find(clientid) != clientsockets.end()))
clientsockets.erase(clientsockets.find(clientid));
if((clientname.count(clientid) != 0) && (clientname.find(clientid) != clientname.end()))
clientname.erase(clientname.find(clientid));
if((clientusername.count(clientid) != 0) && (clientusername.find(clientid) != clientusername.end()))
clientusername.erase(clientusername.find(clientid));
if((clientaddr.count(clientid) != 0) && (clientaddr.find(clientid) != clientaddr.end()))
clientaddr.erase(clientusername.find(clientid));
if((clientcontacts.count(clientid) != 0) && (clientcontacts.find(clientid) != clientcontacts.end()))
clientcontacts.erase(clientcontacts.find(clientid));
if((clientquit.count(clientid) != 0) && (clientquit.find(clientid) != clientquit.end()))
clientquit.erase(clientquit.find(clientid));
if((clientthreads.count(clientid) != 0) && (clientthreads.find(clientid) != clientthreads.end()))
clientthreads.erase(clientthreads.find(clientid));
LOCKED = 0;
break;
}
}
return;
}
這是什麼語言有'每個'? –
我的'Visual C++'用''爲每個'做了'... – IAbstract
Visual C++對每個'擴展名都有一個非標準的':http://msdn.microsoft.com/en-us/library/xey702bw%28VS。 80%29.aspx –