[C++-sig] smart ptr

Roman Yakovenko roman.yakovenko at gmail.com
Sat Jun 2 11:37:47 CEST 2007


On 6/1/07, Sean Ross-Ross <srossross at gmail.com> wrote:
> I have a class that is similar to a smart_ptr, called RefCountPtr
> and I have a c++ class constructor
>
>         X::X( RefCountPtr< foo >& f ) : thisfoo(f) ;
>
> When I pass a Python 'foo' object into this constructor, I want to
> wrap it in a c++  RefCountPtr class before passing it to the c++
> constructor.

What if the instance of foo object is already managed by other RefCountPtr?

> I have written an from_python extration method to do this
>
>
>      static void* extract_rcp(PyObject* o)
>      {
>          object boostobj = object(handle<>(borrowed( o )) );
>          foo *counted = extract<foo*>( boostobj );
>          RefCountPtr< foo > *counter = new RefCountPtr< foo >
> ( counted , true );
>          return counter;
>      }
>
> When I extract the 'foo' object, I create a new RefCountPtr to pass
> to the constructor, once the constructor finishes, I am left with a
> reference count of 2: One for the new'd object and one for the data
> member of the X class.
>
>
> My question is, how can I get rid of the extra reference count? Is
> there a call policy that will do this for me?

I think the whole approach you are taking is erroneous. It is much
better to let Boost.Python to manage such things.

Take a look on "custom smart pointers" guide I wrote few month ago
http://www.language-binding.net/pyplusplus/troubleshooting_guide/smart_ptrs/smart_ptrs.html

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list