tags:

views:

143

answers:

2

I had one UI object attached to a tab that will go missing only under a certain sequence of interaction with the program, and if it's missing, closing the program will result in AV and error of Invalid Window Handle. As I was trying to find out why, I found that calling its .Handle or .HandleNeeded at the start will fix the problem. But that doesn't answer why does it fix the problem.

So I would hope to know that is this common? Has anyone encountered this strange bug before? And know the reason?

+2  A: 

If the control is created during runtime: make sure the parent and owner of the component are set!

birger
+2  A: 

If you're using a TabControl, Delphi only creates the controls for each page when the page is needed (for speed and resource usage reasons). Trying to access controls on a page that hasn't been shown will cause issues, unless you call .HandleNeeded. The call to .HandleNeeded tells Delphi you need the handle now, and eliminates the problem.

It's not a bug, BTW. It's an intentional design decision, for speed and minimization of resource usage as I mentioned above.

Ken White