[C++-sig] Boost.Python and lifetime of instances of C++ classes

Hugo van der Merwe s13361562 at bach.sun.ac.za
Thu Jan 24 10:38:02 CET 2002


> include <iostream.h>
> include <boost/smart_ptr.hpp>
> 
> class test {
> public:
>     test() { std::cout << "test Constructor called." << std::endl; }
>     ~test() { std::cout << "test Destructor called." << std::endl; }
>     void print() { std::cout << "HELLO!" << std::endl; }
>     static boost::shared_ptr<test> create() {
>         return boost::shared_ptr<test>(new test);
>     }
> };
> 
> class foo {
> public:
>     void add(boost::shared_ptr<test> t) { sav = t; }
>     void print() { sav->print(); }
> private:
>     boost::shared_ptr<test> sav;
> };
<SNIP>

Thanks! So making it boost::shared_ptr causes Boost.Python to
automatically call Py_INCREF and Py_DECREF? (I have gone and read about
refcounts, and was wondering how I could write a thin wrapper that gives
me access to the PyObject* as well, so that I can INCREF/DECREF where
appropriate.) Can all of this really be done automatically? If there's
some C++ list class, INCREF should be called in the Add() method, DECREF
should be called in the Remove() method as well as the destructor, for
each of the items in the list... can all of this really be done
automatically?

> ...or you can wait for Boost.Python V2, coming soon, which will handle this
> problem much better.

Soom, soon being couple of months, couple of weeks? Eagerly looking
forward to it, but in the mean time, I like learning about the current
version. (Knowledge gained here is not lost when current version is
superseded by a new version. The knowledge applies in other areas as
well. Also, I'm anyway releasing a wrapper with the current version,
will update it to V2 when it's out.)

Another question: is it possible for a second wrapper to use things from
a first? (I want to make another wrapper, which must be usable at the
same time as the first, but there are e.g. parameters in the second that
are of types wrapped in the first.) I think this is probably not
possible, and the two will have to be combined into one?

Last question: where can I learn more about exceptions? (E.g. how to get
a C++ exception passed on into Python.)

Thanks,
Hugo van der Merwe




More information about the Cplusplus-sig mailing list