实现RAII chromium 有两个类
一个是 RefCounted 类
需要实现引用计数的的类都要继承此类
// A base class for reference counted classes. Otherwise, known as a cheap // knock-off of WebKit's RefCounted<T> class. To use this guy just extend your // class from it like so: // // class MyFoo : public base::RefCounted<MyFoo> { // ... // private: // friend class base::RefCounted<MyFoo>; // ~MyFoo(); // };
使用通过一个scoped_refptr 指针类来管理
View Code
// // A smart pointer class for reference counted objects. Use this class instead // of calling AddRef and Release manually on a reference counted object to // avoid common memory leaks caused by forgetting to Release an object // reference. Sample usage: // // class MyFoo : public RefCounted<MyFoo> { // ... // }; // // void some_function() { // scoped_refptr<MyFoo> foo = new MyFoo(); // foo->Method(param); // // |foo| is released when this function returns // } // // void some_other_function() { // scoped_refptr<MyFoo> foo = new MyFoo(); // ... // foo = NULL; // explicitly releases |foo| // ... // if (foo) // foo->Method(param); // } //
其实用法和OSG的相同,内部实现也应该相同吧。没有去具体看代码。