实际开发中遇到的情况,为了简便写了个简单demo
#include<cstdio>
#include<Windows.h>
#pragma comment(lib, "winmm.lib")
class Test {
int x = 888;
public:
static void CALLBACK funproc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2) {
Test* pthis = (Test*)dwUser;
Sleep(100);//为了特殊情况产生
pthis->fun();
}
void fun() {
printf("x=%d\n", x);
}
};
int main() {
Test* pt = new Test();
UINT timeruid= timeSetEvent(10, 0, Test::funproc, (DWORD_PTR)pt, (UINT)TIME_PERIODIC);
Sleep(20);//为了特殊情况产生
timeKillEvent(timeruid);
if (pt) {
delete pt;
pt = nullptr;
}
getchar();
return 0;
}
主线程执行了delete pt;后,定时器线程调用pthis->fun(),此时pthis指向的空间已经被删除。注意:实际开发中定时器延时是随机的,pt是收到网络信息ack应答后马上删除自身