msg
is a local variable (allocated on stack) and it will be destroyed as soon as the function/method containing that code returns. So, the thread will be accessing invalid memory while using the lparam
.
Some of the solutions could be:
1.) declare 'msg' as static - probably not a good idea
2.) allocate 'msg' on heap, but then you will have to destroy it somewhere
3.) make 'msg' a member variable