Tuesday, December 13, 2011

OwnPtr and PassOwnPtr

I am just trying to jot down my understandings of OwnPtr/PassOwnPtr smart pointer class that is used in WebKit project. There is a good blog explaining the usage of RefPtr and PassRefPtr smart pointers (Refer to http://www.webkit.org/coding/RefPtr.html)

OwnPtr is a simple smart pointer class in which there is only one owner for a pointer created/assigned to OwnPtr.
What it means is that you cannot assign a OwnPtr to another.
But there is a way to transfer the ownership using the PassOwnPtr.
By calling a release on a OwnPtr you get a PassOwnPtr which can be assigned to a OwnPtr.
It can get slightly confusing now OwnPtr is not really a singleton... A Singleton class does not determine the ownership of object created and also OwnPtr does not really prevent one from creating more instances of class.

So how do we create a OwnPtr ->
call adoptPtr(new A) this will give a PassOwnPtr. You can assign this to a OwnPtr and work with it.
How to work between pointers and OwnPtr/PassOwnPtr->
A release on OwnPtr gives you a PassOwnPtr
A leakPtr on OwnPtr gives you Raw Pointer
A leakPtr on PassOwnPtr gives you a Raw Pointer

No comments:

Post a Comment