[C++-sig] Vector of abstract class pointers with boost::python (original) (raw)
Benjamin Kloster benkloster at googlemail.com
Fri Jan 9 20:04:02 CET 2009
- Previous message: [C++-sig] Full Cross-Module Support for Boost Python on Windows
- Next message: [C++-sig] Info on porting to python 3.0
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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:
- 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
- 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 = extractboost::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
- Previous message: [C++-sig] Full Cross-Module Support for Boost Python on Windows
- Next message: [C++-sig] Info on porting to python 3.0
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]