473,803 Members | 3,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(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)

Jul 18 '05 #1
0 1802

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

Similar topics

12
3164
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 an integer. However, as soon as the function is complete, I get a segfault and the interpreter dies. If I run Python interactively, just calling the function causes a segfault. If I'm running a script, I can actually print out the return...
7
2068
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 set<Dog*>. Beforing storing, the Dog pointers are passed through a function taking Animal pointers and dynamic_cast'ing them to Dog pointers. Looks good to me, but it segfaults. Why? Here is my code:
1
2163
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
3527
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
2484
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 segfault. This is legacy code. I tried debugging this problem, and am not able to come up with a valid reason for this. Following function is being used to free: void DBFreePUF (DBPUFRec *userp) {
13
13180
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 following algorithm: ------------------------------------------ public static string Reverse(string s) { if (s == null || s.Length < 2) { return s; }
5
2061
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 just where. Although I'm in quite a self-congratulatory mood right now, in the future, I feel like I could save a lot of time by coercing the interpreter to spew forth method calls to stderr. Is this possible?
3
2702
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 = Py_InitModule ("name", methods); 2. then I get the module's __dict__ structure: dict = PyModule_GetDict (module); 3. Later, I insert a dictionary that I have constructed using the PyDict_* API
2
1295
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 function is working and returning the correct answer. #include <stdio.h> #include <string.h> #include <ctype.h> char* getToken(char *src, unsigned int pos=0) { char *token=new char; unsigned int i=0;
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10550
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10295
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2972
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.