472,353 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 1737

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Nathaniel Echols | last post by:
I've written a function in C to perform protein sequence alignment. This works fine in a standalone C program. I've added the necessary packaging...
7
by: Markus Dehmann | last post by:
I get a segfault in the following scenario (I am not really used to pointers...): I have a base class Animal and a derived class Dog. Now I...
1
by: Java and Swing | last post by:
I am trying to wrap some C code I have. Currently I have something like... defs.h ----------- typedef unsigned long MY_DIGIT; myapp.c...
40
by: Fatih Gey | last post by:
Hi, .. following causes a segfault. .. didn't know why ?! int main() { char name; strcpy (name, "ab8bc8cd8ed"); char cur;
6
by: Code Raptor | last post by:
Folks, I am hitting a segfault while free()ing allocated memory - to make it short, I have a linked list, which I try to free node-by-node. While...
13
by: Aamir Mahmood | last post by:
What could be the fastest method to reverse a string. Speed really matters for my application. Average size of string that comes to my function is...
5
by: James Stroud | last post by:
Hello All, The built-in mac osx vecLib is segfaulting in some cases--A very fun fact to find out the hard way over two nights of work. I also...
3
by: Mitko Haralanov | last post by:
I have a Python module that I have written using the C API and I am having a problem accessing a dictionary from that module. Here is what I have...
2
by: aviraldg | last post by:
I'm having a SEGFAULT on the following program. Weird thing is that it happens randomly with different "str"s (ie. user input; check main() for more...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.