[C++-sig] Vector of abstract class pointers with boost::python
Benjamin Kloster
benkloster at googlemail.com
Fri Jan 9 20:04:02 CET 2009
Hi everyone,
I have the following problem: an abstract base class A that is
exported to python and another class, let's call it L, that is little
more than a glorified vector of A's with a few convenience functions.
The constructor of L is passed a std::vector<A*>. Now I would like to
export L to Python with the following conditions:
1. The __init__ function of L should take a Python list containing sub
classes of A that is automatically converted to a vector of A pointers
2. The L instance takes ownership of the A's (they are deleted in the
destructor)
Since I have to write a wrapper for L anyway (for virtual functions
with default implementation), I tried giving the wrapper class a
constructor like so:
LWrapper(boost::python::object o) {
boost::python::list listOfAs = extract<boost::python::list>(o);
try {
bool is_ok = true;
while(is_ok) {
extract<A*> a(listOfAs.pop(0))
if(a.check()) {
vectorOfAs.push_back(a()) // vectorOfAs is a protected
class member of L
}
else {
is_ok = false;
}
}
}
catch(exception &e) {
std::cerr << "Oops!";
}
}
But this crashes with "glibc dected [...] free(): invalid pointer [...]".
So. How would I actually go about doing this?
Thanks in advance,
Benjamin Kloster
More information about the Cplusplus-sig
mailing list