[webkit-dev] Why are PassRefPtr<>s used as function parameters?

Jens Alfke snej at chromium.org
Tue Oct 27 10:55:56 PDT 2009


Looking at how refcounting is implemented in WebCore, I was surprised  
to find that there are a lot of functions/methods that take  
PassRefPtr<>s as parameters instead of regular pointers to those  
objects. I can't see any benefit to this, and it adds the overhead of  
a ref() and deref() at every call-site.

For example in HTMLNameCollection.h:
   HTMLNameCollection(PassRefPtr<Document>, CollectionType, const  
String& name);

Why shouldn't this be
   HTMLNameCollection(Document*, CollectionType, const String& name);
?

The use of PassRefPtr instead of RefPtr here also seems prone to  
trouble, since inside the implementation of the method it could end up  
unexpectedly clearing the parameter:
   HTMLNameCollection::HTMLNameCollection(Document* doc, .... {
	Ref<Document> otherDoc = doc; // This sets doc to NULL!
	doc->something(); // CRASH
}

I ran across this while trying to track down a reference leak of a  
Document object. As one of my last resorts I set a watchpoint on the  
object's m_refcount to see who refs/derefs it; but I had to give up  
because so many method calls, including the one above, keep constantly  
twiddling the refcount while passing the Document as a parameter.

—Jens


More information about the webkit-dev mailing list