[C++-sig] Custodian & ward

"Jarno Seppänen" js.nospam at wolke7.net
Fri Jun 10 22:00:42 CEST 2005


Hi,

Niall asked:
> ... and each FXWindow instance maintains a list of its children, and
> deletes its children when it gets deleted, then how do I apply 
> custodian & ward to ensure that when the parent instance is deleted, 
> python knows not to delete the children?

I've experienced the same problems when writing wrappers for LLVM.  My
conclusion is that BPL doesn't obey with_custodian_and_ward and/or
return_value_policy in case of constructors.  This is what I think should
work, but eventually this crashes in a double delete:

struct P;
struct C {
    explicit C(P& parent); // transfers ownership of this to parent
};
BOOST_PYTHON_MODULE(init_policy) {
    class_<C, boost::noncopyable>
        ("C", init<P&>()
         [with_custodian_and_ward<2, 1,
              return_value_policy<reference_existing_object> >()]);
}

I've attached a compiling example of the crash, and how to work around by
using a factory function.

BR, Jarno

-- 
Geschenkt: 3 Monate GMX ProMail gratis + 3 Ausgaben stern gratis
++ Jetzt anmelden & testen ++ http://www.gmx.net/de/go/promail ++
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: init_policy.cpp
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20050610/a9da5e5d/attachment.txt>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_init_policy.py
Type: text/x-python
Size: 607 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20050610/a9da5e5d/attachment.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: setup.py
Type: text/x-python
Size: 499 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20050610/a9da5e5d/attachment-0001.py>


More information about the Cplusplus-sig mailing list