I get this weird error:
MyView:OnInitialUpdate()
{
int* my_int;
*(my_int) = 1;
AfxBeginThread(MyThread,my_int);
}
UINT MyThread(LPVOID param)
{
int* my_int = reinterpret_cast<int*>(param);
message(*(my_int));
return 0;
}
void message(int value)
{
CString txt;
txt.Format(_T("%d"),value);
AfxMessageBox(txt);
}
The message box output is 4250636.
Now if I just add another message box before passing the value to the thread:
MyView:OnInitialUpdate()
{
int* my_int;
*(my_int) = 1;
message(*(my_int));
AfxBeginThread(MyThread,my_int);
}
Both message box outputs are 1.
This is undefined behavior. You didn’t initialize
my_int, so you’re dereferencing an invalid pointer.Instead, create a member
xin yourMyViewclass, and change to: