[C++-sig] boost.python: when does python call c++ destructors?

Sebastian Walter walter at mathematik.hu-berlin.de
Thu Feb 28 13:26:24 CET 2008


Hello,
I have compiled a python module with boost.python that exposes the 
functionality of a c++ class that has a constructor and destructor and an 
overloaded operator*. The complete c++ code and wrapper can be found at the 
bottom of this email:

But at first the python script that gives me headaches:

/* test.py */
/*-----------*/
#!/usr/bin/env python
import myclass

a = myclass.asdf()
b = myclass.asdf()

c = a * b
d = a * c

/* output of test.py */
/*-----------------------*/
wronski(walter):~/workspace/code_tests/boost_python> ./test.py
called constructor with loc:    0
called constructor with loc:    1
called operator* with locs:     (0,1)
called constructor with loc:    2
called destructor with loc:     2		?!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
called operator* with locs:     (0,2)
called constructor with loc:    2
called destructor with loc:     2
called destructor with loc:     0
called destructor with loc:     2
called destructor with loc:     1
called destructor with loc:     2

The problem that I don't understand:
a*b calls the constructor of a temporary object that is assigned loc=2
and then directly after that the destructor is called! ...?
I expected the new temporary object to get assigned to the name c!

What bugs me even more is that there are 4 constructor calls but 6!! 
destructor calls! How can this happen?

Is there something im overlooking? Is this intended behavior of 
Python/Boost.Python ?
How could I avoid it?


thanks in advance,

Sebastian Walter




The corresponding boost.python wrapper and c++ code:

/* mainclass.hpp */
/*----------------------*/
#ifndef MAINCLASS_HPP
#define MAINCLASS_HPP

#include <iostream>
#include <vector>
using namespace std;

class asdf{
public:
	int loc;
	asdf();
	~asdf();
	friend const asdf operator*(const asdf &lhs, const asdf &rhs);
};
#endif

/* mainclass.cpp */
/*----------------------*/
#include "mainclass.hpp"

int global_int=0;

asdf::asdf(){
	loc = global_int;
	cout<<"called constructor with loc:\t"<<loc<<endl;
	global_int++;
}

asdf::~asdf(){
	cout<<"called destructor with loc:\t"<<loc<<endl;
	global_int--;
}

const asdf operator*(const asdf &lhs, const asdf &rhs){
	cout<<"called operator* with locs:\t"<<"("<<lhs.loc<<","<<rhs.loc<<")"<<endl;
	return asdf();
}


/* wrapper.cpp */
/*------------------*/
#include "boost/python.hpp"
#include "mainclass.hpp"

BOOST_PYTHON_MODULE(myclass)
{
	using namespace boost::python;
	class_<asdf>("asdf")
		.def(self * self)
	;
}









More information about the Cplusplus-sig mailing list