From: Barry S. <ba...@ba...> - 2008-11-27 15:58:59
|
On 21 Nov 2008, at 21:53, William Newbery wrote: > I want add attributes to my c++ classes that can be used from > python. I know ive got to use the getattr method, however my > implementation crashes on the second access with an error about a > negative reference count. virtual Py::Object getattr(const char *name) { if(std::string("cls2")==name)return Py::asObject(cls2); else return getattr_methods(name); } asObject takes ownership of cls2. And when the ref count drops to 0 cls2 is deleted. You then pass a pointer to a deleted object ot asObject as get a crash. I suggest that you keep cls2 is a Py::Object so that you keep a ref count of 1 so long as Cls() exists. I added std::cout to the c'tor and d'tor of your objects to prove this was the case. Barry |