[C++-sig] problems with embedded python and pure virtual functions

Birgir Sigurjonsson birgir-s at online.no
Thu Jan 4 14:22:09 CET 2007


Hi, I am having problem calling a pure virtual function that
is overritten in Python. My apology, if this post is to long
but I am not sure how to formulate my question nor do I know
what part of my code is needed to for you all to help me.

I can create my AbstractLoader and call its load function.
But then load calls load_impl I get Abort in the console.

Therfor I think that there is something wrong with my
wrapper and or the auto_ptr.

I have stepped through the code in debugger and here is the call stack:

ZN5boost6python23throw_error_already_setEv,                              FP=7fbfffe960
boost::python::expect_non_null<_object>,                                  FP=7fbfffe980
boost::python::detail::manage_ptr<_object>,                               FP=7fbfffea50
boost::python::handle<_object>::handle<_object>,                          FP=7fbfffea80
boost::python::detail::method_result::method_result,                      FP=7fbfffeaa0
boost::python::override::operator ()<std::string>,                        FP=7fbfffeb00
AbstractLoaderWrap::load_impl,                                            FP=7fbfffeb60
AbstractLoader::load,                                                     FP=7fbfffeb90


Here is my wrapper:

class AbstractLoaderWrap : public AbstractLoader, public wrapper<AbstractLoader> {
public:
  AbstractLoaderWrap(PyObject* self_) : m_self(self_) { Py_INCREF(m_self); }
  virtual ~AbstractLoaderWrap () { Py_DECREF(m_self); }

  virtual bool load_impl(const std::string& path) {
    return this->get_override("load_impl")(path);
  }

  PyObject *m_self;
};

BOOST_PYTHON_MODULE(import_framework) {
    class_<AbstractLoader, std::auto_ptr<AbstractLoaderWrap>, boost::noncopyable>("AbstractLoader")
      .def("load_impl",  pure_virtual(&AbstractLoader::load_impl))
    ;
    implicitly_convertible<std::auto_ptr<AbstractLoaderWrap>, std::auto_ptr<AbstractLoader> >();

    class_<AbstractLoaderFactoryWrap, boost::noncopyable>("AbstractLoaderFactory", init<std::string>())
        .def("make",  pure_virtual(&AbstractLoaderFactory::make), return_value_policy<manage_new_object>())
    ;
}

Here is my base class:

class AbstractLoader : private boost::noncopyable {
public:
  AbstractLoader();
  bool load(const std::string& path);
  virtual ~AbstractLoader();
  virtual bool load_impl(const std::string& path) = 0;
};

bool AbstractLoader::load(const std::string& path) {
  std::cout << "AbstractLoader::load -" << path << "\n";
  bool status = load_impl(path);
  return status;
}

Any help appreciated,
Birgir Sigurjonsson.



More information about the Cplusplus-sig mailing list