470,647 Members | 1,186 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,647 developers. It's quick & easy.

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


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.
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: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)

Jul 18 '05 #1
0 1689

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Nathaniel Echols | last post: by
7 posts views Thread by Markus Dehmann | last post: by
1 post views Thread by Java and Swing | last post: by
40 posts views Thread by Fatih Gey | last post: by
6 posts views Thread by Code Raptor | last post: by
13 posts views Thread by Aamir Mahmood | last post: by
5 posts views Thread by James Stroud | last post: by
3 posts views Thread by Mitko Haralanov | last post: by
2 posts views Thread by aviraldg | last post: by
1 post views Thread by Korara | last post: by
reply views Thread by warner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.