Originally Posted by
bengreenwood
And finally, you delete pCtrl->Paint (); from beneath case WM_PAINT: in the message loop.
If you delete the pCtrl object in a WM_PAINT msg then the next msg generated by windows will crash your app.
Only delete this object in response to the WM_DESTROY msg.
When the window is created a instance of the Controller object is allocated. A pointer to this object is stored under the window, in a location called 'user data'.
Each time a msg is sent through the callback, a pointer to the Controller object is retrieved from the windows 'user data' using GetWindowLong and the msg's HWND.
If you delete the pCtlr object, but do not 'clear' the HWNDs user data, the next msg will get an invalid (dangling) pointer passed to it (by Controller * pCtrl = WinGetLong<Controller *> (hwnd))
You will then call a method on an object instance you have deleted (== crash)
Originally Posted by
bengreenwood
Ultimately I'm trying to make the program automatically paint stuff on the screen at different times.
Look at 'timers'.
You create a timer
Process the WM_TIMER msgs (it fires every time interval, not just once)
Kill the timer when finished with it (ie WM_DESTROY or once fired)
Code:
//pure WIN32 API, you will need to modify for that wrapper
//global define of timer ID number (resouce.h)
#define IDT_TEXT_REFRESH 101
//WM_CREATE
//create a timer to fire each second called IDT_TEXT_REFRESH
//that will send WM_TIMER msgs to the main callback (wndproc for the HWND we give it)
SetTimer(hWnd, IDT_TEXT_REFRESH, 1000, NULL); //times are in ms (this is not high resolution, >100 ms)
//WM_TIMER
//process the timer msgs
//check which timer has fired
if(wParam == IDT_TEXT_REFRESH)
//refresh the text etc
//note kill the timer in WM_TIMER if you only want it to fire once
//WM_DESTROY
//end the timer
KillTimer(hWnd, IDT_TEXT_REFRESH);