|
i686,SWIG+Perl, segmentation fault when using C structures: msg#00141programming.swig
Hi, when I tried to create a Perl module wich uses SWIG wrappers for C functions several problems popped up: 1. After the module is loaded the Perl script produces segmentation fault. After I removed a simple statement like "my $x=3.14;" it does not segfault anymore, but gives an error "free(): invalid pointer". Also the output for the Perl script is garbled. 2. If instead of working in linux-i686 machine I do the same on sparc-solaris, the situation is just opposite: without that statement Perl script produces "Bus Error", while works almost fine with the statement included. On Solaris, though, another strange hing happen - you have to quote variables like $Cstruct::cstruct->{key}, otherwise a null pointer is passed to SWIG function. Printing the same variable using Perl's print works fine. The details are given below. The Perl script SwigTest.pl loads Modules Cstruct.pm and SwigTest.pm (produced by SWIG). Cstruct.pm has a $cstruct variable which is initialized using cstruct_make(), a SWIG wrapper around malloc(). The content of the $cstruct is printed using print_string(), a SWIG wrapper around printf(). Environment: Linux: uname -a: i686, SuSE 9.1, kernel 2.6.5-7.108-default, perl -v: v5.8.3 built for i586-linux-thread-multi, swig -version: SWIG Version 1.3.21, Compiled with g++ [i686-pc-linux-gnu] Solaris: uname -a: SunOS 5.8 Generic_117000-01 sun4u sparc, perl -v: v5.8.5 built for sun4-solaris swig -version: SWIG Version 1.3.22, Compiled with g++ [sparc-sun-solaris2.8] To run the example: swig -perl -shadow SwigTest.i; gcc -D_GNU_SOURCE -g -fPIC -mcpu=i686 -O3 -I/usr/include -I/usr/local/include -I/usr/lib/perl5/5.8.3/i586-linux-thread-multi/CORE -c SwigTest_wrap.c SwigTest.c; ld -G SwigTest.o SwigTest_wrap.o -o SwigTest.so; ./Swigtest.pl; You will have to adjust the path to perl executable and to perl CORE directory. Here is the full source: //--------------SwigTest.c--------------// #include <stdio.h> #include "SwigTest.h" void print_string(const char * str) { if(0==str) printf("ZERO POINTER!\n", str); else if(0==*str) printf("Empty string!\n", str); else printf("%s\n", str); } cstruct_t *cstruct_make() { cstruct_t *cstruct; cstruct=(cstruct_t *)malloc(sizeof(cstruct_t)); return(cstruct); } //--------------SwigTest.h--------------// typedef struct cstruct { char *key; } cstruct_t; void print_string(const char * str); cstruct_t * cstruct_make(); //--------------SwigTest.i--------------// %module SwigTest %{ #include <stdio.h> #include "SwigTest.h" %} typedef struct cstruct { char *key; } cstruct_t; %name(print_string) extern void print_string(const char *str); %name(cstruct_make) extern cstruct_t * cstruct_make(void); //--------------Cstruct.pm--------------// package Cstruct; use strict; use SwigTest; use vars qw($hr $cstruct); # This package provides access to two variables # One is an ordinary perl hash, with one element # The other is a hash tied via SWIG to a C structure containing one element my %hash; $hr =\%hash; # commenting (or uncommenting) the following statement will cause # crashes in Perl script that loads this module !!! my $x=3.14; $hr->{key} = "Contents of hash element with key \"key\""; $cstruct=SwigTest::cstruct_make() or die; $cstruct->{key}="Contents of hash element linked via Swig to C structure element"; print "[Loaded: ", __FILE__, "]\n"; 1; //-----------Swigtest.pl----------------// #!/usr/gnu/bin/perl -w use strict; use lib ("."); use Cstruct; use SwigTest; print "\n\n"; print "\$Cstruct::hr is a $Cstruct::hr\n"; print "\$Cstruct::cstruct is a $Cstruct::cstruct\n"; print "\n"; print "\$Cstruct::hr->{key} contains: "; print $Cstruct::hr->{key},"\n"; print "\$Cstruct::cstrcut->{key} contains: "; print $Cstruct::cstruct->{key},"\n"; print "\n"; print "When \$Cstruct::hr->{key} is passed to C it contains: "; SwigTest::print_string($Cstruct::hr->{key}); print "When \$Cstruct::cstruct->{key} is passed to C it contains: "; SwigTest::print_string($Cstruct::cstruct->{key}); print "When \"\$Cstruct::cstruct->{key}\" is passed to C it contains: "; SwigTest::print_string("$Cstruct::cstruct->{key}"); print "\n"; //--------------------------------------// _______________________________ Do you Yahoo!? Declare Yourself - Register online to vote today! http://vote.yahoo.com _______________________________________________ Swig maillist - Swig@xxxxxxxxxxxxxxx http://mailman.cs.uchicago.edu/mailman/listinfo/swig |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | complex output argument: 00141, Bob Marinier |
|---|---|
| Next by Date: | How to return a std::vector of pointers to Java?: 00141, Henrietta Slack |
| Previous by Thread: | complex output argumenti: 00141, Bob Marinier |
| Next by Thread: | How to return a std::vector of pointers to Java?: 00141, Henrietta Slack |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |