[C++-sig] Shared pointers and new objects

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Sun Feb 1 21:01:36 CET 2009

I had trouble with this myself a few years ago:


(My reaction was a redesign of my parent/child types, so shared pointers
don't explicitly appear in the interfaces anymore.)

----- Original Message ----
From: Eric Jonas <jonas at MIT.EDU>
To: cplusplus-sig at python.org
Sent: Sunday, February 1, 2009 7:37:51 AM
Subject: [C++-sig] Shared pointers and new objects

I am trying to return a shared pointer to a class Foo and then 
test the results in python for equality, but they always seem to fail.
I've created the following two trivial classes: 

class Foo : public boost::noncopyable

typedef boost::shared_ptr<Foo> pFoo_t; 

class FooCreator
  pFoo_t createFoo() {
    pFoo_t ft(new Foo); 
    intfoo = ft; 
    return ft; 
  pFoo_t getFoo() {
    return intfoo; 

  pFoo_t intfoo; 


That I expose with boost::python via: 

  class_<Foo, pFoo_t, boost::noncopyable>("Foo", no_init); 
    .def("createFoo", &FooCreator::createFoo)
    .def("getFoo", &FooCreator::getFoo); 

Yet the following assert fails:

fc = FooCreator()

foo = fc.createFoo()
foo2 = fc.getFoo()

assert_equal(foo, foo2)

The above assert fails. I've tried using the newest boost::python, I've
looked through the mailing list archives and even asked on IRC, but I
still can't figure out which part of this process I'm doing incorrectly.
I've spent about 20 hours reducing my problem to this test case, and am
really stumped. 


Cplusplus-sig mailing list
Cplusplus-sig at python.org

More information about the Cplusplus-sig mailing list