|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- PostQueuedCompletionStatus(hIOCP, sizeof(IOCP_PARAM), (ULONG_PTR)new IOCP_PARAM(IocpListPop, "hellp world",func), NULL);
- HANDLE hThread = (HANDLE)_beginthread(threadQueueEntry, 0, hIOCP);
- =========================================================================
- void threadQueueEntry(HANDLE hIOCP)
- {
- threadmain(hIOCP);
- _endthread();
- }
- void threadmain(HANDLE hIOCP)
- {
- std::list<std::string> lstString;
- DWORD dwTransferred = 0;
- ULONG_PTR CompletionKey = 0; //这个是new出来的
- OVERLAPPED* pOverlapped;
- while (GetQueuedCompletionStatus(hIOCP, &dwTransferred, &CompletionKey, &pOverlapped, INFINITE))
- {
- IOCP_PARAM* pParam = (IOCP_PARAM*)CompletionKey;
- if ((dwTransferred == 0) && (CompletionKey == NULL))
- {
- //delete pParam; //这里是我的想法
- break;
- }
- if (pParam->nOperator == IocpListPush)
- {
- lstString.push_back(pParam->strData);
- }
- else if (pParam->nOperator == IocpListPop)
- {
- std::string* pStr = NULL;
- if (lstString.size() > 0)
- {
- pStr = new std::string(lstString.front());
- lstString.pop_front();
- }
- if (pParam->cbFunc)
- {
- pParam->cbFunc(pStr);
- }
- }
- else if (pParam->nOperator == IocpListEmpty)
- {
- lstString.clear();
- }
- delete pParam;
- }
- //_endthread();
- }
复制代码
我的理解是上面CompletionKey异常时,break直接结束循环没有调用到delete才会导致内存泄漏,所以在出口那里加上delete就可以了。
还有种说法是因为线程在执行_endthread()的时候结束了,所以来不及析构线程中的变量,导致内存泄漏。间接调用有作用域,所以出作用域就会全部析构掉里面的变量
|
|