Hello all

While I was trying to use TICSWndControl as base class to share handles for 
message and consume less OS resources I ran into a very strange problem, that 
took me some nights of sleep, after a lot of researching I have found a problem 
in WndControl shared handles implementation, and I made an example program that 
shows the problem

I will try to explain the situation

The main problem is in the management of registered message queue, when you 
create a new object it will register its message to the message assignment 
queue by using AllocateMsgHandlers, so I create I component and it will assign 
the message 1025 to FMsgRelease, if I create another one, it will assign 
message 1026 to FMsgRelease of the other control

If I Post message 1025 to the first component, just after posting I free it, it 
will release 1025 from the message queue, making it available for use, and then 
I create a new component (It will assign 1025 for the new control). When the 
message is received, it will be received by the third component, but I sent the 
message to the first one!

In my demo program I simulate the following situation:

I have two types of classes (TMyControl and TMyControl2)

On TMyControl I register a test message
TMyControl2 is simple descendent of TICSWndControl, the only thing is that it 
will call AllocateHWnd on constructor, so it will allocate the release message

The path to error:

- Create one instance of TMyControl (Messages 1025 and 1026 will be reserved 
for it (1025 = Release, 1026 = Test)
- Create another instance of TMyControl (Messages 1027 and 1028 will be 
reserved for it)
- Post message 1026 to first instance (1026 is a test message)
- Free first instance of TMyControl (That was supposed to receive the message, 
but messages 1025 and 1026 will be released and free to be used for another 
- Create an instance of TMyControl2 (Message 1025 will be assigned to it as 
- Create an instance of TMyControl2 (Message 1026 will be assigned to it as 

Ok, when the handle received message 1026 (That was a message supposed to be 
received by an instance of TMyControl) it will be actually received by the 
second instance of TMyControl2, which will release the control since 1026 is 
now mapped to FMsgRelease message!!

Do you have any suggestion of fix?

I thought that when releasing a message handler we must in some way clean the 
message queue if there is any message in windows queue that was directed to 
that component, but how to do it?

The link for test application is:

Thank you very much
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to