I'm trying to make a very simple extension which will return a new copy
of a C++ string object to python, and I'm segfaulting in the process.
I'm using Python 2.2.2, SWIG 1.3.17 and g++ 3.3.
Am I getting something very basic wrong? See code below.
-David
/* file: dups.cxx */
#include "dups.h"
string * dupstring(strin g s) {return new string(s);}
/* file: dups.h */
#ifndef __DUPS_H__
#define __DUPS_H__
#include <string>
using namespace std;
string * dupstring(strin g s);
#endif
/* file: dups.i */
%module dups
%{
#include "dups.h"
%}
/* Convert from C++ --> Python */
%typemap(out) string {
$result = PyString_FromSt ring($1.c_str() );
}
/* Convert from Python --> C++ */
%typemap(in) string {
$1 = PyString_AsStri ng($result);
}
string * dupstring(strin g);
/* file: makefile */
CFLAGS = -gstabs+ -O3 -fpic
_dups.so: dups.o dups_wrap.o
g++ -shared -o $@ $?
%.o: %.cpp
g++ -c $(CFLAGS) -o $@ $?
%.o: %.cxx
g++ -I/usr/include/python2.2 -c $(CFLAGS) -o $@ $?
%_wrap.cxx: %.i
swig -c++ -python $?
clean:
rm -f *.o dups_wrap.* *.so dups.py dups.pyc
/* file: session_crash */
Python 2.2.2 (#1, Mar 21 2003, 23:40:29)
[GCC 3.2.3 20030316 (Debian prerelease)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
import dups
a = dups.dupstring( "foo")
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 16384 (LWP 22607)]
0x0fcc87ec in strlen () from //lib/libc.so.6
(gdb) bt
#0 0x0fcc87ec in strlen () from //lib/libc.so.6
#1 0x0fb41324 in _wrap_dupstring (self=0x0, args=0x0)
at /usr/include/c++/3.3/bits/char_traits.h:1 43
#2 0x100ba9e8 in PyCFunction_Cal l ()
#3 0x1003c000 in Py_MakePendingC alls ()
#4 0x1003cd4c in PyEval_EvalCode Ex ()
#5 0x1003fdcc in PyEval_EvalCode ()
#6 0x1006cd34 in PyRun_FileExFla gs ()
#7 0x1006c19c in PyRun_Interacti veOneFlags ()
#8 0x1006bf1c in PyRun_Interacti veLoopFlags ()
#9 0x1006d984 in PyRun_AnyFileEx Flags ()
#10 0x1000c4c8 in Py_Main ()
#11 0x1000bf28 in main ()
#12 0x0fc62da4 in __libc_start_ma in (argc=1, ubp_av=0x7ffff9 f4, ubp_ev=0x0, auxvec=0x7ffffa 5c,
rtld_fini=0x300 26b38 <_rtld_local> , stinfo=0x1000bf 18, stack_on_entry= 0x7ffff9e0)
at ../sysdeps/powerpc/elf/libc-start.c:186
(gdb)