Updating main dialog from thread ( crash in RELEASE )

Hi guys

Currently I am updating my main dialog from a thread. And the program only crash when it is running in RELEASE build. It runs perfectly fine in DEBUG build !!! which make it very difficult for me to debug :(

I have search through the forum and realise that I am actually already doing according to the recommended way when updating UI from a thread. I got the information from here
How to access UI elements from a thread in MFC? (http://www.dev-archive.com/forum/showthread.php?t=312454)

I have just done a simple program following the concept above and it really crash when in RELEASE build.

The application will crash when you press the "Button1" more than once and then close the application. If you just press it once and close it, it will not crash. Sometimes it will crash when you press "Button1" for alot of times.

Can anybody help me...I have posted my code...it is very small only. Can you see if it will crash on your machine?

What I wrote was:
1. When "Button1" is press, disable "Button2" and create a thread using AfxBeginThread passing in the "GetSafeHwnd" as a parameter to the thread

2. In the thread function, it will sleep for 2 seconds before posting a message using ::PostMessage to the main dialog to enable "Button2"

Thats all.
Thanks
[1355 byte] By [justin0108] at [2007-11-20 2:57:38]
# 1 Re: Updating main dialog from thread ( crash in RELEASE )
This is a very common mistake. In your function definition and declaration, you did not account for the WPARAM and LPARAM required.

In the header fileafx_msg void enableButton2();should beafx_msg LRESULT enableButton2(WPARAM, LPARAM);
and in your cpp filevoid CThreadDlg::enableButton2()
{
mButton2.EnableWindow( true );
}should beLRESULT CThreadDlg::enableButton2(WPARAM, LPARAM)
{
mButton2.EnableWindow( true );
return 0; // normally will be 0 to indicate no error
}
Hope that helps.
krmed at 2007-11-10 23:10:56 >
# 2 Re: Updating main dialog from thread ( crash in RELEASE )
Hi Kermed !!!
It really works!!! :thumb:

But why is it so important to account for the 2 variables?? Because anyway I am passing NULL to them. And why is it that with or without the 2 variables the same function will still get called?

Not sure whether this is a stupid question. If it is really that important to have the 2 variables why shouldn't the compiler catch the mistake?

Thanks :D
justin0108 at 2007-11-10 23:12:01 >
# 3 Re: Updating main dialog from thread ( crash in RELEASE )
But why is it so important to account for the 2 variables?? Because anyway I am passing NULL to them. And why is it that with or without the 2 variables the same function will still get called?

Not sure whether this is a stupid question. If it is really that important to have the 2 variables why shouldn't the compiler catch the mistake?

The compiler can't catch the error as you are sending him a pointer. Nothing more nothing less. And generally speaking functions in C/C++ can be viewed as pointers but not pointers to data but pointers to code. The arguments are prepared on the stack and a jump is made to that address.

So, for a function declared as having int 2 args there will be 8 bytes on stack ( assuming int is 4 bytes) when the function is reached. Once the function exits these 8 bytes need to be removed from the stack and the program will continue from the adddress currentlly at the top of the stack. Something like:

arg2
arg1
next ->this is where the program will correctlly continue

But since you are not providing the arguments nothing will be popped out and the "next" address will be an incorrect value.

arg2 ->this is where the program will try to continue
arg1
next

Hope I've managed a decentlly clear explanation. :)
PadexArt at 2007-11-10 23:13:06 >
# 4 Re: Updating main dialog from thread ( crash in RELEASE )
Hi PadexArt

There is one more thing which I do not understand. Why is it that the function pointer that I am passing to windows, in this case "enableButton2" can be of any signature??

I always thought that when we pass function pointer into a function, that function has already specifically state that how the function signature should look like.

For example in the AfxBeginThread, if the "threadFunc" function pointer that I pass to it does not follow the convention "UINT threadFunc( LPVOID )", it will throw me an error.

But in the "enableButton2" case, when the function is define with 0 arguments, the compiler did not complain. That is why I thought that I can have any number of parameters that I want.

And also the functions generated by the wizard ( when i double click on a button )...for eg "OnButton" has 0 arguments. This is also one of the reason why I code my "enableButton2" with 0 arguments.

So how do i know when to have 0 and when to have 2 arguments?

Thanks.
justin0108 at 2007-11-10 23:14:06 >
# 5 Re: Updating main dialog from thread ( crash in RELEASE )
There is one more thing which I do not understand. Why is it that the function pointer that I am passing to windows, in this case "enableButton2" can be of any signature??
Look at how the message map is implemented.

So how do i know when to have 0 and when to have 2 arguments?
You need to consult the documentation.
PadexArt at 2007-11-10 23:15:00 >
# 6 Re: Updating main dialog from thread ( crash in RELEASE )
Thanks for your explanation earlier PandexArt. :thumb:

I have found the documentation regarding the number of arguments too. For those who are interested or face the same problem as me :p

http://msdn2.microsoft.com/en-us/library/k35k2bfs(VS.80).aspx
justin0108 at 2007-11-10 23:16:04 >