I'm passing information between threads (one managed, one
not) and if something gets mixed up so there are still
messages in flight (PostMessage) when I call
DestroyWindow, I'd like to not lose anything more than
just leaking a small buffer object (up to 200 bytes) on
the heap. Since the .NET gc can't track pointers inside
message queues, this is how I've chosen to do it.
That is in violation of the docs for PostMessage:
"If you send a message in the range below WM_USER to the
asynchronous message functions (PostMessage,
SendNotifyMessage, and SendMessageCallback), its message
parameters cannot include pointers. Otherwise, the
operation will fail. The functions will return before the
receiving thread has had a chance to process the message
and the sender will free the memory before it is used."
In other words, you shouldn't fire off pointers to a
window and expect the window to delete them. Instead, you
should use SendMessage, and delete them yourself. This
then avoids the whole issue.
way. It says that if you want pointer marshalling to work
message functions. You still can use pointers as message
parameters for messages above WM_USER. Just don't expect any
free marshalling services from system. As long as you post
moment there is no problem with PostMessage.