[C++-sig] Re: (no subject)

David Abrahams dave at boost-consulting.com
Mon Sep 29 13:31:00 CEST 2003

"Andrew Ellem" <alellem at uwaterloo.ca> writes:

>> Andrew,
>> Clearly the error must be coming from somewhere else, for example a
>> destructor of one of your objects which uses a HANDLE.  None of the
>> Boost.Python code manipulates any type called HANDLE.
> You are correct in that it is my code that is causing the problem.
> More
> specifically, it is that Boost.Python is deleting one of my objects
> when I don't want it to.
> My problem is this: In my class's constructor, I create a class that
> is implemented in Python.  The constructor takes a pointer to the
> calling class.  Some pseudo-code for clarity's sake:
> class X { X() { Y y = PythonClass( this ) } }
> The problem is that after the __init__ method is called, Boost tries
> to clean up the arguments, which causes my calling class to be
> deleted, hence the crash.  So my problem is how do I prevent this?
> I'm guessing the problem is that by default Python passes by value,

No, Python passes by reference always.

> and so does Boost.  How do I do otherwise?  I'm guessing
> call-policies, but in all honesty I'm having some trouble getting my
> head around them all.

Call policies are only for when Python is calling your C++ function.
Does this help?  http://www.boost.org/libs/python/doc/v2/ptr.html

>> Yes, it seems like you're taking shots in the dark.  Have you tried
>> reading the documentation on Call Policies and/or stepping through
>> the code that's crashing with a debugger?
> I've read the documentation, but I admit I don't understand it 100%.
> From what I've read, I need to use a precall policy that tells Boost
> not to manage the variable I'm passing.  But I'm really not sure which
> policy does this, or even one exists.
> I've been trying to find an example, the closest I can find is from
> the init.html example, which uses with_custodian_and_ward.  Is this
> what I want to do?  I don't really want to just throw it in and see if
> it works.
> Perhaps a better question, where can I find a thorough explanation of
> the way Python, and Boost.Python handle arguments?

Unfortunately nowhere.

Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list