[C++-sig] Instantiate a wrapped class in C++ and get the PyObject pointer

Stefan Seefeld seefeld at sympatico.ca
Mon Apr 23 22:47:45 CEST 2007


David Sveningsson wrote:
>> object a_instance = A(...); // Create an A instance and wrap it in a
>> python object
>>
>> PyObject *ptr = a_instance.ptr(); // access a C API pointer from the
>> instance
>>
> 
> But if I return a_instance.ptr() the object will be corrupt right? But if
> I allocate on the heap it will leak memory?

Can you rephrase the question, please ? The underlaying pointer is owned by
the boost::python::object instance, no matter how often you access it via
the ptr() member function. Thus, if you want to hand a reference to it to
someone else, you have to make sure the reference counter is appropriately
incremented (Read boost.python's reference guide for more on the topic.)

Regards,
		Stefan

-- 

      ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list