[C++-sig] eposing classes with protected destructors

Daniel Paull dlp at fractaltechnologies.com
Mon Dec 23 04:05:32 CET 2002


Hi all,

I've just updated to the latest CVS version of boost.python and am
having trouble exposing classes with protected destructors.  The classes
in the library that I am wrapping have protected destructors to enforce
the use of the a ref-counting mechanism for object lifetime management.

Previously I had done the following to get Python/C++ to work together:

------------------------
template< class T >
class RefCountedObject
{
public:
	typedef T element_type;
	explicit RefCountedObject( T* t ) : m_pPtr( t )
	{
		if ( m_pPtr ) m_pPtr->addRef();
	}
	
	~RefCountedObject()
	{
		if ( m_pPtr ) m_pPtr->releaseRef();
	}
	
	T& operator*() const { return *get(); }
	T* operator->() const{ return get(); }
	T* get() const { return m_pPtr; }
private:
	T* m_pPtr;
};

class_< WrappedClass, RefCountedObject< WrappedClass >, ... >( ... )
	...
	;
------------------------

Here the RefCountedObject simply increments the refcount of the C++
object to hold it alive while Python is using it.  This reference is
released when the Python Object (and hence, the RefCountedObject) is
destroyed.

I thought this was pretty neat, though I'd be happy to get advice on a
better solution if one exists.

The compile error I get with the latest CVS version is that the
destructor of "WrappedClass" is inaccessible at line 22 of
boost/python/detail/destroy.hpp.

I've tracked this back to rvalue_from_python_data.hpp (line 132):

---------------
template <class T>
inline rvalue_from_python_data<T>::~rvalue_from_python_data()
{
    if (this->stage1.convertible == this->storage.bytes)
        python::detail::destroy_referent<ref_type>(this->storage.bytes);
}
---------------

I won't pretend to know all the details of what is going on here, but I
have an inkling that the "if" test would fail in this case, but
obviously, the code for the referent destroyer must be compiled anyhow.

Any suggestions?


Cheers,

Daniel Paull







More information about the Cplusplus-sig mailing list