Menu

#1191 Segfault when looping a std::list from python

None
closed-fixed
nobody
python (260)
5
2022-02-04
2011-09-02
No

A segmentation fault is generated, when looping a wrapped public std::list from python, where the wrapper code was generated with the "-threads" argument.

E.g.:
== foo.cpp ==
#include <string>
#include <list>

class foo {
public:
foo() : bar() {}
std::list<std::string> bar;
};
==========

== foo.i ==
%module foo

%include <stl.i>
%include <std_list.i>

%template(StringList) std::list<std::string>;

%{
#include "foo.cpp"
%}
%include "foo.cpp"
========

$ swig -c++ -python -threads -o foo_wrap.cpp foo.i
$ g++ -fPIC -c foo_wrap.cpp -I<PYTHON_INCLUDE_PATH>
$ g++ -shared foo_wrap.o foo.o

$ python
>>> import foo
>>> f = foo.foo()
>>> print f.bar
<foo.StringList; proxy of <Swig Object of type 'std::list< std::string,std::allocator< std::string > > *' at 0x1004d0de0> >
>>> f.bar.append("test")
>>> f.bar[0]
'test'
>>> for i in f.bar:
... print i
...
test
Segmentation fault

Discussion

  • Martin Skou Andersen

    Last g++ command is wrong. it should be:
    $ g++ -shared foo_wrap.o -o _foo.so -l<PYTHON_LIBNAME>.

     
  • Martin Skou Andersen

    I find that if I comment/remove the SWIG_PYTHON_THREAD_BEGIN_ALLOW and SWIG_PYTHON_THREAD_END_ALLOW macros in the _wrap_delete_SwigPyIterator method then no segmentation fault is generated.

     
  • Olly Betts

    Olly Betts - 2022-02-04
    • status: open --> closed-fixed
    • Group: -->
     
  • Olly Betts

    Olly Betts - 2022-02-04

    The testcase works with current SWIG git master so it seems the cause has been fixed in the meantime.

     

Log in to post a comment.

MongoDB Logo MongoDB