Re: PostMessage and unprocessed messages
"Giovanni Dicanio" <giovanni.dicanio@invalid.com> ha scritto nel messaggio
news:%23KBxRQPgIHA.5164@TK2MSFTNGP03.phx.gbl...
However, I must confess that this [WFSO] seems to me not the best
solution...
I've also tried another solution, i.e. building a "custom memory manager",
it's a template class, which exposes three main methods: New, Delete, and
Clear.
Something like this:
template <typename T>
class MemoryMgr
{
public:
MemoryMgr();
~MemoryMgr();
T * New();
void Delete( T * & );
void Clear();
...
};
MemoryMgr< MyMessageData > g_messageDataMgr;
The message data is created by the sender thread using New, like this:
MyMessageData * data = g_messageDataMgr.New();
and when the data is consumed, it is deleted using Delete:
// Receiver:
... process data
// No more needed
g_messageDataMgr.Delete();
Even if there are message pending, calling the MemoryMgr destructor or Clear
method deletes all pending heap pointers.
The key here is that memory management is centralized into this class.
No leak: that is OK.
BTW: I've also tested creating some data using new BYTE[ 20000000 ] and
*not* deleting[] them... Windows does proper cleanup when the process
terminates, so it seems that no memory is leaked when process exits. So also
C++ has a kind of built-in (primitive) garbage collector :)
Giovanni