SWIG Python gives segfault on char * argument

Discussion in 'Python' started by ObiWanKinobi, Jun 6, 2010.

  1. ObiWanKinobi

    ObiWanKinobi

    Joined:
    Jun 6, 2010
    Messages:
    1
    I have a code written in C++ for a modified version of SRILM. The filenames are srilm.c and srilm.h . I wish to build the Python module _srilm.so from it so that I can test my module on python. The Makefile is as follows:
    Code:
    SRILM_LIBS=/cs/natlang-sw/packages/srilm-1.5.7/lib/x86_64-bit-generic
    SRILM_INC=/cs/natlang-sw/packages/srilm-1.5.7/include
    PYTHON_INC=/cs/natlang-sw/Linux-x86_64/NL/LANG/PYTHON/2.6.2/include/python2.6
    PERL_INC=/usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE
    
    python: clean _srilm.so
    
    _srilm.so: srilm.o srilm_python_wrap.o
       g++ -shared $^ -loolm -ldstruct -lmisc -L$(SRILM_LIBS) -o $@
    
    srilm_python_wrap.o: srilm_python_wrap.c
       g++ -c -fpic $< -I/usr/local/include/ -I$(SRILM_INC) -I$(PYTHON_INC)
    
    srilm_python_wrap.c: srilm_python.i
       swig -python $<
    While compiling srilm_python_wrap.c, I get the following errors:
    Code:
    srilm_python_wrap.c: In function ‘int SWIG_Python_ConvertFunctionPtr(PyObject*, void**, swig_type_info*)’:
    srilm_python_wrap.c:2034: error: invalid conversion from ‘const char*’ to ‘char*’
    srilm_python_wrap.c: In function ‘int SWIG_AsCharPtrAndSize(PyObject*, char**, size_t*, int*)’:
    srilm_python_wrap.c:2648: error: cannot convert ‘int*’ to ‘Py_ssize_t*’ for argument ‘3’ to ‘int PyString_AsStringAndSize(PyObject*, char**, Py_ssize_t*)’
    srilm_python_wrap.c: In function ‘void SWIG_Python_FixMethods(PyMethodDef*, swig_const_info*, swig_type_info**, swig_type_info**)’:
    srilm_python_wrap.c:3699: error: invalid conversion from ‘const char*’ to ‘char*’
    make: *** [srilm_python_wrap.o] Error 1
    To get around it I manually typecasted the arguments and executed the command to get _srilm.so
    My test code in python looks like this:
    Code:
    #!/usr/bin/env python2.4
    
    # Use the srilm module
    from srilm import *
    
    # Initialize a trigram LM variable (1 = unigram, 2 = bigram and so on)
    n = initLM(5)
    
    # Read 'sample.lm' into the LM variable
    print "Loading the LM file ..."
    #readLM(n, "sample.lm")
    readLM(n, "/cs/natlang-data/wmt10/lm/eparl_nc_news_2m.en.lm")
    print "Loading complete!!"
    the readLM() function segfaults.
    The corresponding code for srilm_python_wrap.c generated by swig is as follows:
    Code:
    SWIGINTERN PyObject *_wrap_readLM(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
      PyObject *resultobj = 0;
      Ngram *arg1 = (Ngram *) 0 ;
      char *arg2 = (char  *) 0;
      int result;
      void *argp1 = 0 ;
      int res1 = 0 ;
      int res2 ;
      char *buf2 = 0;
      int alloc2 = 0 ;
      PyObject * obj0 = 0 ;
      PyObject * obj1 = 0 ;
    
      if (!PyArg_ParseTuple(args,(char *)"OO:readLM",&obj0,&obj1)) SWIG_fail;
      res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_Ngram, 0 |  0 );
      if (!SWIG_IsOK(res1)) {
        SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "readLM" "', argument " "1"" of type '" "Ngram *""'"); 
      }
      arg1 = (Ngram *)(argp1);
      res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2);
      if (!SWIG_IsOK(res2)) {
        SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "readLM" "', argument " "2"" of type '" "char const *""'");
      }
      arg2=buf2;
      result = (int)readLM(arg1,(char const *)arg2);
      resultobj = SWIG_From_int((int)(result));
      if (alloc2 == SWIG_NEWOBJ) free((char*)buf2);
      return resultobj;
    fail:
      if (alloc2 == SWIG_NEWOBJ) free((char*)buf2);
      return NULL;
    }
    The problem seems to be the variable buf2 is corrupt. If I hardcode arg2 with some value, the code works just fine. Also, if I try to print buf2 or arg2, the code segfaults. Also, even if i write:
    arg2 = (char *)buf2;
    the code still segfaults.Basically, wherever I assign buf2 to something or try to print buf2, the code segfaults.Can someone help point out what is wrong here and how I might get the code to work?
    ObiWanKinobi, Jun 6, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Eger
    Replies:
    0
    Views:
    379
    David Eger
    Jul 11, 2003
  2. Bill Davy
    Replies:
    0
    Views:
    687
    Bill Davy
    May 12, 2005
  3. sethukr@gmail.com

    GCC gives SEGFAULT... but GDB runs

    sethukr@gmail.com, Mar 2, 2007, in forum: C Programming
    Replies:
    6
    Views:
    581
    William Hughes
    Mar 2, 2007
  4. Andrey Vul
    Replies:
    8
    Views:
    683
    Richard Bos
    Jul 30, 2010
  5. Marcos Prieto
    Replies:
    1
    Views:
    461
    Antoine Pitrou
    Aug 5, 2010
Loading...

Share This Page