Re: Delphi Movie #17 - Owner vs. Parent



You now have me nervous about what I have said in this video, I might need to update it or follow it up as you suggest.

Harold Howe [TeamB] wrote:
Alister Christie wrote:
My latest video is online. It's a discussion of Owner vs. Parent. Have a look on my website http://codegearguru.com

Years ago, I spent considerable time waging a crusade against a common misconception of the Owner and Parent properties. That misconception is that Owners always handle cleanup, and that the Parent property is only for display purposes. In reality, this simplistic viewpoint is not the whole story.

Before you create a follow up to this video, you might want to consider stepping through the VCL destruction code of the form in your sample. Pay careful attention to what happens when you reach TWinControl.Destroy. There you will find a loop that iterates over the Controls array of the form, calling Destroy on each item.

Put a breakpoint on the call to Destroy inside the loop. Keep running until the Instance object is a panel. Then step into it and keep stepping. Eventually, you will end up back inside TWinControl.Destroy, but this time, in the context of the panel (Self is TPanel). The same loop executes, and what you should see, is the panel calling Destroy on its child button control: ie, a control being deleted by its Parent, not its Owner.

If you already know this, I apologize for explaining something you already understand. My experience in these groups has been that most people don't know this, and a large percentage of them don't believe me until they step through the debugger themselves. Of course, the VCL could have changed since I last chased down this windmill in 2000. But probably not. This is pretty fundamental to how things work. The general idea is that child controls can't outlive their parents. At least, I think that is the idea.

For more on this, do a groups search on articles posted by me with the words Owner and Parent in them. Or simply search on TWinControl.Destroy.

H^2


--
Alister Christie
http://codegearguru.com
.


Loading