[C++-sig] Re: strange class_.def behaivior

David Abrahams dave at boost-consulting.com
Fri Oct 10 03:47:00 CEST 2003


"Mike Rovner" <mike at nospam.com> writes:

> Hi,
>
> I have an exception during loading a bpl extension module.
> When bpl try to execute class_.def statement, class namespace object pointer
> = 0.
> In file src/object/function.cpp line 388 (add_to_namespace()) ns
> uninitialized,
> because class_ this->m_ptr = 0.
>
> It looks like my mistake but where?

You're returning a dangling reference to a local temporary from
PyClass.  That's a no-no.

> Regards,
> Mike
>
>
> Example code:
>
> #include <boost/python.hpp>
> using namespace boost::python;
>
> struct Test
> {
>   Test(int s=0) : size(s) {}
>   int Size() const {return size;}
>
>   int size;
> };
>
> static bool test_eq(const Test& t1, const Test& t2) { return t1.Size() ==
> t2.Size(); }
>
> template<class T>
> struct Wrap
> {
>   static class_<T>& PyClass(const char* name)
>   {
>     return
>     class_<T>(name)
>       .def(init<int>())
>       .def("__len__", &T::Size)
>       ;
>   }
> };
>
> BOOST_PYTHON_MODULE(ext1)
> {
>   Wrap<Test>::PyClass("Test").def("__eq__", &test_eq);
> }

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list