From: Brian B. <Bri...@se...> - 2008-08-13 14:56:20
|
Thanks! That seems to work perfectly. Now I just need to look into prototyping to find out *why* that works! :) > -----Original Message----- > From: Matthew O'Meara [mailto:mat...@gm...] > Sent: 13 August 2008 14:29 > To: Brian Bird; swi...@li... > Subject: Re: [Swig-user] 64-bit Segmentation Fault with Swig 1.3.36 > > Try using this interface file-- > > > > /* file /tmp/example.i */ > > %module example > > %{ > extern char *test(char *, char *); > > %} > > extern char *test(char *, char *); > > > Best, > Matt > > > On Wed, Aug 13, 2008 at 9:13 AM, Brian Bird > <Bri...@se...> wrote: > > > > The function in example_wrap is below (I've added // ** LINE 2631 ** to > > highlight the line showing the warning during compilation): > > > > example_wrap.c: In function '_wrap_test': > > example_wrap.c:2631: warning: cast to pointer from integer of different > > size > > > > I tried attaching the complete example_wrap files generated by both Swig > > versions 1.3.36 and 1.3.16 but they were too large and the mailing list > > doesn't accept zip attachments. If they're any use I could send them to > > you directly? > > > > Version 1.3.16 generates a much smaller function (also reproduced below) > > and works successfully, but it has an identical line to cast the result > > to a (char *), so I've no idea what's going wrong. As far as I can tell > > the same *_wrap.c files are generated on a 32 bit machine. > > > > Thanks! > > Brian > > > > > > /* Output in example_wrap.c from SWIG 1.3.36 */ > > SWIGINTERN PyObject *_wrap_test(PyObject *SWIGUNUSEDPARM(self), PyObject > > *args) { > > PyObject *resultobj = 0; > > char *arg1 = (char *) 0 ; > > char *arg2 = (char *) 0 ; > > int res1 ; > > char *buf1 = 0 ; > > int alloc1 = 0 ; > > int res2 ; > > char *buf2 = 0 ; > > int alloc2 = 0 ; > > PyObject * obj0 = 0 ; > > PyObject * obj1 = 0 ; > > char *result = 0 ; > > > > if (!PyArg_ParseTuple(args,(char *)"OO:test",&obj0,&obj1)) SWIG_fail; > > res1 = SWIG_AsCharPtrAndSize(obj0, &buf1, NULL, &alloc1); > > if (!SWIG_IsOK(res1)) { > > SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "test" "', > > argument " "1"" of type '" "char *""'"); > > } > > arg1 = (char *)(buf1); > > res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); > > if (!SWIG_IsOK(res2)) { > > SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "test" "', > > argument " "2"" of type '" "char *""'"); > > } > > arg2 = (char *)(buf2); > > result = (char *)test(arg1,arg2); // ** LINE 2631 ** > > resultobj = SWIG_FromCharPtr((const char *)result); > > if (alloc1 == SWIG_NEWOBJ) free((char*)buf1); > > if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); > > return resultobj; > > fail: > > if (alloc1 == SWIG_NEWOBJ) free((char*)buf1); > > if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); > > return NULL; > > } > > > > > > > > > > > > > > > > /* Output in example_wrap.c from SWIG 1.3.16 */ > > static PyObject *_wrap_test(PyObject *self, PyObject *args) { > > PyObject *resultobj; > > char *arg1 ; > > char *arg2 ; > > char *result; > > > > if(!PyArg_ParseTuple(args,(char *)"ss:test",&arg1,&arg2)) goto fail; > > result = (char *)test(arg1,arg2); > > > > resultobj = result ? PyString_FromString(result) : > > Py_BuildValue((char*)""); > > return resultobj; > > fail: > > return NULL; > > } > > > > > > > >> -----Original Message----- > >> From: Haoyu Bai [mailto:div...@gm...] > >> Sent: 13 August 2008 13:23 > >> To: Brian Bird > >> Cc: swi...@li... > >> Subject: Re: [Swig-user] 64-bit Segmentation Fault with Swig 1.3.36 > >> > >> On Wed, Aug 13, 2008 at 7:08 PM, Brian Bird > >> <Bri...@se...> wrote: > >> > I'm getting a Segmentation fault on a 64-bit Ubuntu machine when > > using > >> > Swig1.3.36 and Python2.5. > >> > > >> > > >> > > >> > When compiling my example_wrap.c I get a warning about "casting to > >> pointer > >> > from integer of different size". I assume this is the cause of the > >> segfault. > >> > > >> > > >> > > >> > Everything works on a 32-bit machine. There is no warning (since > >> pointers > >> > and integers are the same size). > >> > > >> > > >> > > >> > However the wrapper works fine when generated from swig version > > 1.3.16 > >> (on > >> > both 32-bit and 64-bit machines). > >> > > >> > > >> > > >> > I tried Swig1.3.33 and it fails in the same way. Has something > > broken in > >> > Swig since version 1.3.16 when returning strings (char *) to Python? > > Or > >> is > >> > there a better way to return a string from a C function to python > > which > >> will > >> > ensure the latest versions of swig will generate wrappers which work > > on > >> > 64-bit machines? Most of the documentation/forums I've read seem to > > be > >> aimed > >> > either at Swig1.1 or using typedefs/structs which seems (IMHO) to be > >> > overcomplicated just to return a string to Python - isn't it? > >> > > >> > > >> > > >> > Any suggestions would be appreciated! > >> > > >> > > >> > > >> > Below is the simplest example I could create which shows the > > problem. > >> > Thanks! > >> > > >> > > >> > > >> > /* file /tmp/example.i */ > >> > > >> > %module example > >> > > >> > extern char *test(char *, char *); > >> > > >> > > >> > > >> > /* file /tmp/example.c */ > >> > > >> > char *test(char *param1, char *param2) { > >> > > >> > return param2; > >> > > >> > } > >> > > >> > > >> > > >> > % cd /tmp > >> > > >> > % tar -xzf swig-1.3.36.tar.gz > >> > > >> > % cd swig-1.3.36/ > >> > > >> > % ./configure --with-python=/usr/bin/python2.5 --prefix=/tmp/swig && > >> make && > >> > make install > >> > > >> > % cd .. > >> > > >> > % /tmp/swig/bin/swig -python example.i # Creates example_wrap.c and > >> > example.py > >> > > >> > % gcc -c -fpic example_wrap.c example.c -I/usr/include/python2.5 > >> > > >> > example_wrap.c: In function '_wrap_test': > >> > > >> > example_wrap.c:2631: warning: cast to pointer from integer of > > different > >> size > >> > > >> > % gcc -shared example.o example_wrap.o -o _example.so # Creates > >> _example.so > >> > > >> > % python > >> > > >> > Python 2.5.2 (r252:60911, Apr 21 2008, 11:17:30) > >> > > >> > [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2 > >> > > >> > Type "help", "copyright", "credits" or "license" for more > > information. > >> > > >> >>>> import example > >> > > >> >>>> print example.test("Hello","World") # Should print "World" on the > >> >>>> screen. It works on 32-bit machines and with SWIG-1.3.16 > >> > > >> > Segmentation fault > >> > > >> > >> Could you provide more information likes the line of the code > >> generated the compile warning, or the trace back of the segmentation > >> fault? I havn't a 64-bit Linux so I can't debug it.However, I believe > >> it is somewhere the pointer of a 32-bit int casted to a 64-bit size_t. > >> > >> -- > >> Haoyu Bai > > > > ------------------------------------------------------------------------ > - > > This SF.Net email is sponsored by the Moblin Your Move Developer's > challenge > > Build the coolest Linux based applications with Moblin SDK & win great > prizes > > Grand prize is a trip for two to an Open Source event anywhere in the > world > > http://moblin-contest.org/redirect.php?banner_id=100&url=/ > > _______________________________________________ > > Swig-user mailing list > > Swi...@li... > > https://lists.sourceforge.net/lists/listinfo/swig-user > > |