[C++-sig] shared_ptr trouble
Ralf W. Grosse-Kunstleve
rwgk at yahoo.com
Sat Mar 11 12:43:23 CET 2006
I'm having terrible trouble understanding how Boost.Python interacts with
shared_ptr. I'm trying to implement a small tree node class with parent-child
relationships via weak_ptr:
class node : boost::noncopyable
{
private:
node() {}
boost::weak_ptr<node> parent_;
public:
std::string id;
// ...
};
To be used eventually like this:
node1 = node.create(id="a")
node2 = node.create(id="b", parent=node1)
node1 has no parent, node2 should use node1 as the parent. The equivalent C++
code is:
std::string
exercise_child_parent_id()
{
boost::shared_ptr<node> node1 = node::create("a");
boost::shared_ptr<node> node2 = node::create("b", node1);
return node2->get_parent_id();
}
A minimal, self-contained example is attached. The test is simply:
from boost_adaptbx_tree_ext import node, exercise_child_parent_id
def exercise():
print "C++:", exercise_child_parent_id()
node1 = node.create(id="a")
node2 = node.create(id="b", parent=node1)
print "Python:", node2.get_parent_id()
if (__name__ == "__main__"):
exercise()
Output:
C++: a
Python: no parent
The C++ code produces the desired result, but the Python version fails. By
adding print statements to this function of the node class
static
boost::shared_ptr<node>
create(
std::string const& id,
boost::shared_ptr<node> const& parent)
{
boost::shared_ptr<node> result(new node);
result->id = id;
result->parent_ = parent;
return result;
}
I found out that result->parent_->id is in fact "a" right before the return,
but back in Python the parent is lost. Since node is noncopyable I cannot
figure out how this can be, and what I need to do to fix the problem. I'd be
grateful for advice!
This is with Python 2.4, gcc 3.4 under Fedora Core 3 x86_64, using the Boost
CVS from yesterday.
Cheers,
Ralf
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: tree_ext.cpp
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20060311/7adec8c9/attachment.txt>
More information about the Cplusplus-sig
mailing list