Dave, Here is the reduced example with only the `boost::enable_shared_from_this` variant. The issues are all as reported before.
-Holger On Mon, Apr 23, 2012 at 22:25, Dave Abrahams <[email protected]> wrote: > > on Mon Apr 23 2012, Holger Brandsmeier <brandsmeier-AT-gmx.de> wrote: > >> In fact the statement >> class_<IWrapper, boost::shared_ptr<IWrapper>, boost::noncopyable >>>("I", init<>() ) >> has an issue. When I call getThisRCP() later, and the return value is >> not a null shared pointer, then I in fact the error >> TypeError: No to_python (by-value) converter found for C++ type: >> boost::shared_ptr<I> >> which is quite annoying. I also implemented Dave's suggestion with >> boost::enable_shared_from_this and because of the above issue I used >> this for the Wrapper and not the class itself (see K and KWrapper in >> the attachment). >> >> The behaviour of `enable_shared_from_this` is different from my >> initual getThisRCP() / setThisRCP() solution but still has seriour >> problems. Concider the code: >> >> class KDer(K): >> def __init__(self): >> K.__init__(self) >> >> k = KDer() >> >> In contrast to my initial solution the lines >> assert k.getThisRCP2() is not None >> k2 = k.getThisRCP2() >> now work, which is nice. >> >> Also this call works now: >> print k2.foo(); >> >> However, the following two lines >> del k >> print k2.foo(); >> give a segmentation fault. Note that the destructor for the class `K` >> has not been called, which would have printed a statement. > > Please post a reduced example without any of the "J" business that > illustrates exactly the problem you're having for this one particular > case. If doing so doesn't immediately reveal the problem to you, I'm > sure we can get to the bottom of it here. > > > -- > Dave Abrahams > BoostPro Computing > http://www.boostpro.com > > _______________________________________________ > Cplusplus-sig mailing list > [email protected] > http://mail.python.org/mailman/listinfo/cplusplus-sig
test_rcp.py
Description: Binary data
#include <boost/python.hpp>
#include "iostream"
#include "string"
#include "boost/weak_ptr.hpp"
#include "boost/enable_shared_from_this.hpp"
using namespace boost::python;
using namespace std;
//using Teuchos::RCP;
struct K
// : public boost::enable_shared_from_this<K>
{
virtual int foo() = 0;
virtual ~K() {
std::cout << "destructor for K called ..." << std::endl;
}
/*boost::shared_ptr<K> getThisRCP() {
return shared_from_this();
}*/
};
struct KWrapper : public K, public boost::python::wrapper<K>,
public boost::enable_shared_from_this<KWrapper>
{
KWrapper() {
}
virtual int foo() {
if( override f = this->get_override("foo") ) {
return f();
} else {
return base_foo();
}
}
boost::shared_ptr<KWrapper> getThisRCP2() {
return shared_from_this();
}
int base_foo() {
return 0;
}
};
BOOST_PYTHON_MODULE(thisRCPPy)
{
{
typedef K ClassT;
class_<KWrapper, boost::shared_ptr<KWrapper>, boost::noncopyable >("K", init<>() )
//.def("getThisRCP", &ClassT::getThisRCP)
.def("getThisRCP2", &KWrapper::getThisRCP2)
.def("foo", &ClassT::foo)
;
}
}
_______________________________________________ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
