使用此关键字在结构中引用类时出错 [英] Trouble referencing class in structure using this keyword
问题描述
头文件:
// 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);
}
我有一个简单的问题,我不能把我的手指
I've got an easy problem here that I can't put my finger on.
- 我有一个线程的参数,它传递一个包含类的结构。
- 我实例化结构与
这
,然后在线程启动时传递 - 我尝试解除引用
- 当我添加行以访问类中的某个内容时,应用程序崩溃。 >
- I have params for a thread, which pass a struct which contains a class.
- I instantiate the struct with
this
and then pass it when the thread starts - I try to dereference (?) it in the worker function.
- At this point, everything compiles OK.
- When I add lines to access something in the class, the app crashes.
我的错误在哪里?
推荐答案
你传递的局部栈变量超出范围 StartThread()
返回。因此,您引用的堆栈空间不再属于您。
You're passing a local stack variable that is out-of-scope the moment StartThread()
returns. You are therefore referencing stack-space that no longer belongs to you.
void MyClass::StartThread() {
temp = 1234;
DATA data = {this}; // << LOCAL VARIABLE OUT OF SCOPE ON FUNCTION EXIT
HANDLE hThread = (HANDLE) _beginthreadex(0, 0, &WorkerFunction, &data, 0, 0);
// Commented out for now while I address the current problem
//CloseHandle(hThread);
}
动态分配数据,或者更好地使数据成为 MyClass
并传递此
作为线程数据。在你的case你只传递*这反过来通过一个结构,所以只是传递它作为参数
Either dynamically allocate the data, or better still, make the data a member of MyClass
and pass this
as the thread data. In your case you're only passing *this anyway through a struct, so just pass it as the param
void MyClass::StartThread() {
temp = 1234;
HANDLE hThread = (HANDLE) _beginthreadex(0, 0, &WorkerFunction, this, 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);
MyClass* cls0 = static_cast<MyClass*>(param0);
etc...
}
您的线程过程,它必须具有有效的生命周期所需的持续时间。由线程。要么确保通过将动态分配的所有权传递给线程,让它进行删除,或者在线程事件中为它的使用寿命传递一个已知的保持确定状态的指针。它显示
满足后者,所以你应该很好去。
Whatever you pass to your thread procedure, it must have valid lifetime for the duration required. by the thread. Either ensure that by passing ownership of a dynamic allocation to the thread at let it do the delete, or pass a pointer known hold determinate state for the lifetime of its usage in the thread-proc. It appears this
fulfills the latter, so you should likely be good to go.
这篇关于使用此关键字在结构中引用类时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!