1
頭文件引用的結構類:故障使用此關鍵字
// Free function to use in thread
unsigned int __stdcall WorkerFunction(void *);
class MyClass {
public:
int temp;
void StartThread();
}
typedef struct {
MyClass * cls;
} DATA;
CPP類:
void MyClass::StartThread() {
temp = 1234;
DATA data = {this};
HANDLE hThread = (HANDLE) _beginthreadex(0, 0, &WorkerFunction, &data, 0, 0);
// Commented out for now while I address the current problem
//CloseHandle(hThread);
}
unsigned int __stdcall WorkerFunction(void * param0) {
MessageBox(NULL, "WorkerFunction()", "Alert", MB_OK);
DATA * data = (DATA *) param0;
MyClass* cls0 = data->cls;
// Crashes when reference to cls0 is attempted.
char buf[5];
snprintf(buf, 5, "%i", cls0 ->temp);
MessageBox(NULL, buf, "Alert", MB_OK);
}
我有一個簡單的問題,在這裏,我不能把我的手指上。
- 我有一個線程,它傳遞一個包含類的結構的參數。
- 我使用
this
實例化結構,然後在線程啓動時傳遞它 - 我嘗試在worker函數中取消引用(?)它。
- 在這一點上,一切都編譯好。
- 當我添加行來訪問類中的某些東西時,應用程序崩潰。
我的錯誤在哪裏?
Dammit範圍每次都讓我。有關如何解決的任何建議? – Ben
乾杯,我有我自己的幾個,但他們可能吠叫錯了樹...只有我發現後,我已經離開了森林:) – Ben
而且我們有擡頭。非常感謝。這比我可恥的小堆垃圾代碼heheh要好上百萬倍。 – Ben