segfault dup'ing string through C++ (via swig) module

D

David Eger

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(string s) {return new string(s);}


/* file: dups.h */
#ifndef __DUPS_H__
#define __DUPS_H__

#include <string>
using namespace std;
string * dupstring(string s);

#endif


/* file: dups.i */
%module dups

%{
#include "dups.h"
%}

/* Convert from C++ --> Python */
%typemap(out) string {
$result = PyString_FromString($1.c_str());
}

/* Convert from Python --> C++ */
%typemap(in) string {
$1 = PyString_AsString($result);
}

string * dupstring(string);


/* 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.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:143
#2 0x100ba9e8 in PyCFunction_Call ()
#3 0x1003c000 in Py_MakePendingCalls ()
#4 0x1003cd4c in PyEval_EvalCodeEx ()
#5 0x1003fdcc in PyEval_EvalCode ()
#6 0x1006cd34 in PyRun_FileExFlags ()
#7 0x1006c19c in PyRun_InteractiveOneFlags ()
#8 0x1006bf1c in PyRun_InteractiveLoopFlags ()
#9 0x1006d984 in PyRun_AnyFileExFlags ()
#10 0x1000c4c8 in Py_Main ()
#11 0x1000bf28 in main ()
#12 0x0fc62da4 in __libc_start_main (argc=1, ubp_av=0x7ffff9f4, ubp_ev=0x0, auxvec=0x7ffffa5c,
rtld_fini=0x30026b38 <_rtld_local>, stinfo=0x1000bf18, stack_on_entry=0x7ffff9e0)
at ../sysdeps/powerpc/elf/libc-start.c:186
(gdb)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top