Messages sent with PostMessage getting lost

Giganews Newsgroups
Subject: Messages sent with PostMessage getting lost
Posted by:  Ross McMillan (nospam@gmail.com)
Date: Mon, 17 Oct 2005

I'm writing an Indy app that send emails in parallel from threads.  I wanted
the threads to report back progress, bytes sent, etc regularly and I had
done this by accessing strings in the thread from the VCL loop.  Now, after
being wrapped over the knuckles by Martin James (and quite rightly too I now
realise) I am in the process of reporting back progress by PostMessage.

Just to satisfy my curiousity I wrote a test program that started a thread
and furiously assigned to a string from within the thread, and then checked
the string from the main loop (via Application.OnIdle).  As expected, every
so often (but not very often), the string as seen by the main loop is not
what it should be.  Also, and very much less often, I get an AV.  Whatever,
confirmation that it's not a good idea.

Next step was to try posting the string back.  I create a record for the
string with New in the thread and pass it's address as a message parameter
and post it off.

In the main thread message handler I grab the string, check it, and release
the memory that was used to transport it back.  As expected, it runs without
errors in the received string.

However...

Just to really push things, I didn't send off one message in the thread - I
send off 1000 at a time and follow with a sleep so that the main thread gets
a chance to handle the messages.  I keep track of messages posted and
messages delivered and find that over time, I lose some of them.  Presumably
the storage I create to transport the string gets lost also - i.e. I leak
memory.

What's going on here?  Can we not assume that all messages posted make it to
the handler?

Replies