[webkit-dev] DOM tree surgery and DOM tree destruction

Darin Adler darin at apple.com
Wed Jun 25 10:15:04 PDT 2008


On Jun 25, 2008, at 10:06 AM, Pitaga wrote:

> For example, suppose that a browser based on WebKit has loaded a Web  
> page and parsed it, producing a DOM tree T. As an exercise, I want  
> to perform surgery on T and then destroy both T and the constituents  
> that the surgery removed from T, without leaking. For purposes of  
> this exercise, the browser session itself is of no interest.

You don't explicitly destroy anything. DOM objects will be destroyed  
when the last owner goes away; that's what reference counting is used  
for.

So "How do I destroy the tree?" is the wrong question. If there's a  
test case where something's not getting destroyed, you could ask "Why  
isn't it being destroyed?" but there's no need to write code to  
explicitly destroy anything.

> What's the best practice here with respect to raw pointers,  
> PassRefPtr's, and RefPtr's?

That question is too vague and broad for me to answer. Maybe you could  
ask a more specific question?

> How do I destroy the subtree dominated by W after I remove it?

I had a lot of trouble following the letters in your example, so I'm  
not sure about "W" or even what "dominated" means.

Generally if you remove a child with removeChild, then that child and  
all its descendants will be destroyed when the last reference to it  
goes away. Typically you are holding a RefPtr to the child you are  
removing; in that case the most likely time it will be destroyed is  
when that RefPtr goes out of scope. Or you might only have a raw  
pointer. In that case, it's likely the child will destroyed within the  
removeChild function.

> How do I destroy T when I'm all finished?

You say that "WebKit has loaded a web page and parsed it [to produce  
T]". Given that, T is a document and it's owned by the Frame that  
loaded it. As long as its the current document in that Frame it will  
be kept alive. When the Frame either goes away or loads a new  
document, then it will be destroyed, unless someone else is holding a  
reference to it. When the last reference to it goes away, it will be  
destroyed.

     -- Darin



More information about the webkit-dev mailing list