From: Paul P. <pog...@gm...> - 2006-12-30 17:19:45
|
Hi, I have this in my SWIG interface file. Intention is to use templated C++-level container for storing arbitrary Python objects (in addition to uses in C++ code.) However, this simple test fails with a segmentation fault: c = Color_list (3, "ss") c[WHITE] = 3.3 print c[WHITE] print 3.3 * 3 Note that if second 3.3 is replaced with other number, it all works fine. I think the reason of failure is that there are no Py_INCREF() and Py_DECREF() for stored/retrived objects in resulting `.cpp' file. Am I doing something wrong? Is there a generic solution? Paul %include "python/pyobject.swg" %{ ... %} enum Color { NONE, WHITE, BLACK }; template <typename type> class Color_array { public: Color_array (const type white_value, const type black_value); }; %extend Color_array { type __getitem__ (Color key) { return (*self) [key]; } void __setitem__ (Color key, type value) { (*self) [key] = value; } }; %template (Color_list) Color_array <PyObject*>; |
From: Josh C. <jc...@nc...> - 2006-12-31 20:21:34
|
On Sat, 30 Dec 2006, Paul Pogonyshev wrote: > Note that if second 3.3 is replaced with other number, it all works > fine. I think the reason of failure is that there are no Py_INCREF() > and Py_DECREF() for stored/retrived objects in resulting `.cpp' file. > > Am I doing something wrong? Is there a generic solution? I think you've answered your own question: if you're writing your own code that uses the Python C API, you need to take care of reference counting. You certainly can't expect SWIG to take care of it for you (how could it?). Josh |