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

Daniel Paull dlp at fractaltechnologies.com
Tue Dec 24 02:57:19 CET 2002


> -----Original Message-----
> From: c++-sig-admin at python.org [mailto:c++-sig-admin at python.org] On
Behalf
> Of David Abrahams
> Sent: Monday, 23 December 2002 9:15 PM
> To: c++-sig at python.org
> Subject: Re: [C++-sig] eposing classes with protected destructors
> 
> "Daniel Paull" <dlp at fractaltechnologies.com> writes:
> 
> It looks like vc6 doesn't give the backtrace.

VC and templates are always a drag.

> 
> Nope.  Unfortunately it's all irrelevant.
>

Upon re-reading my mail, I agree.  Sorry ;)

> 
> > Get ready to be surprised - this all worked great with a CVS version
> > from a month or two back.  I think I last updated in the second half
of
> > November.  I can't vouch for 1.29.0 though.
> 
> Show me a small example, please. I still can't believe it.

The code below compiles fine against 1.29.0, but fails as per my
original post when compiled against yesterdays CVS version.

----------------SNIP-------------------------
#include <boost/python.hpp>
using namespace boost::python;

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;
};

// needed for latest CVS version of boost.python
namespace boost { // dang VC
template<class T> T * get_pointer( RefCountedObject<T> const& p )
{
    return p.get();
}
}

class A
{
public:
	A( bool val = false ) : m_refCount( 0 ) { m_value = val; }
	virtual void setValue( bool val ) { m_value = val; }
	virtual bool getValue() const { return m_value; }
	virtual void addRef () { ++m_refCount; }
	virtual void releaseRef () { if( !--m_refCount ) delete this; }
	virtual int getRefCount () { return 0; }	
protected:
	virtual ~A() {}
	bool m_value;
};

BOOST_PYTHON_MODULE( foo )
{
	class_< A, RefCountedObject< A > >( "A", init< bool >() )
		.def( "setValue", &A::setValue )
		.def( "getValue", &A::getValue )
		;
}
----------------SNIP-------------------------

Cheers,

Dan






More information about the Cplusplus-sig mailing list