Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What external events trigger TCustomForm.RecreateWnd?

A particular VCL application is crashing, rarely, and apparently spontaneously, and I strongly suspect it is because the main form's window handle is being recreated in response to some external trigger from the operating system.

I do know for certain that if the main form's window handle were to change then trouble would certainly follow (because background threads post messages to this handle).

But I would like to know how to reproduce this bug before I set about making code changes intended to fix it, if only so I can confirm that this is a plausible cause of the crashes, that the symptoms match those reported, and that the code changes do indeed fix it. (Also if there is no way the OS can trigger a call to TCustomForm.RecreateWnd I need to find some other explanation for the crashes. Yes, I could post CM_RECREATEWND to the main form but that just begs the question.)

(I am aware that the correct way to post messages from background threads to the VCL thread is to create a hidden message window purely for this purpose using AllocateHWnd - that is my proposed fix.)

I have seen a similar error with a descendent of TCustomListView where RecreateWnd was being triggered by changing the desktop style from Windows XP to Windows Classic. But I haven't been able yet to find such a method to trigger RecreateWnd on a TCustomForm. (I've spent a considerable amount of time reading through the VCL code and I must be missing something.)

Summary:

Under what stimuli from the operating system does the VCL end up calling RecreateWnd on a TCustomForm? (I'm especially interested in Windows 7 if that makes a difference.)

If there is such a stimulus from the OS, I can prove whether it causes the observed symptoms. If there definitively isn't then I can prove window recreation isn't the cause.

like image 461
Ian Goldby Avatar asked Dec 10 '14 12:12

Ian Goldby


1 Answers

Windows will destroy your form if it is owned or parented to another window that is being destroyed.

That will cause your form's handle to be destroyed.

But since your form is still around, Delphi will re-create the handle the next time.

like image 97
Ian Boyd Avatar answered Sep 23 '22 07:09

Ian Boyd