473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1781

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 to use it in Python; it returns three strings and...
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 construct two Dogs and store pointers to them in a...
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 ------------- void MakeDigits(MY_DIGIT digits) {
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 free()ing the 28th node (of total 40), I hit a...
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 around 200K - 300K. Currently i am using the...
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 spent an embarrassing amount of time figuring out...
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 done: 1. In my init function I call module =...
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 info) and my debugger shows me that the getToken...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.