Loading ...
Sorry, an error occurred while loading the content.

Boost::Python constructor overloading problems

Expand Messages
  • John Hunter
    I have been using boost python for some time now and recently upgraded to boost_1_28_0 and gcc 3.0.4. I started getting a segmentation fault when using a
    Message 1 of 5 , Jun 2, 2002
    • 0 Attachment
      I have been using boost python for some time now and recently upgraded
      to boost_1_28_0 and gcc 3.0.4. I started getting a segmentation fault
      when using a library with an overloaded constructor. In trying to
      track down the source of the problem, I wrote a simple class with an
      overloaded constructor and still get the seg fault.

      Perhaps there is something wrong with code or the compile statement,
      but I have been unable to find. it. So I've also included some
      additional info below that may give someone a clue as to what is going
      wrong.

      Here is the python script which calls the simple class. The
      second constructor crashes with a seg fault:

      from TestSimple import Simple

      # Default constructor OK
      d1 = Simple()
      print d1.get()

      i = 1
      d2 = Simple(i) # this line causes the seg fault
      print d2.get()

      ----
      Here is the code I use to make the module TestSimple:

      #include <boost/python/class_builder.hpp>

      class SimpleClass {
      public:

      SimpleClass() : i(-1) {};
      SimpleClass(int i) : i(i) {};
      int get() const { return i; }
      private:
      const int i;
      };

      namespace python = boost::python;

      BOOST_PYTHON_MODULE_INIT(TestSimple)
      {
      python::module_builder m( "TestSimple" );
      python::class_builder<SimpleClass> simple(m, "Simple");

      simple.def(python::constructor<>());
      simple.def(python::constructor<int>());
      simple.def(&SimpleClass::get, "get");
      }

      ----

      Here are the compile commands:

      /usr/local/bin/g++ -I/usr/local/include/python2.1 -I/usr/local/include -ggdb -Wall -fPIC -ftemplate-depth-21 -c TestSimplePy.cpp
      /usr/local/bin/g++ -shared -L/usr/local/lib -ggdb -Wall -fPIC -ftemplate-depth-21 TestSimplePy.o -o TestSimple.so -lboost_python

      ----

      I run the script in a controlled environment with:
      #!/bin/sh
      export LD_LIBRARY_PATH=/usr/local/lib
      /usr/local/bin/python2.1 ./testSimple.py

      ----

      Here is the ldd for the TestSimple module:
      mother:~/c/trade/test> ldd TestSimple.so
      libboost_python.so.1.28.0 => /usr/local/lib/libboost_python.so.1.28.0 (0x40011000)
      libstdc++.so.3 => /usr/local/lib/libstdc++.so.3 (0x40078000)
      libm.so.6 => /lib/i686/libm.so.6 (0x40112000)
      libgcc_s.so.1 => /usr/local/lib/libgcc_s.so.1 (0x40136000)
      libc.so.6 => /lib/i686/libc.so.6 (0x4013e000)
      libutil.so.1 => /lib/libutil.so.1 (0x4026e000)
      /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000)

      ----

      All the version info looks right -- python2.1 was compiled with the
      same gcc (3.0.4) that the module was.

      Thanks for any suggestions,
      John Hunter
    • David Abrahams
      I can t reproduce your problem with gcc-3.0.4 or gcc-3.1 Unzip the enclosed user directory to your libs/python directory and run bjam there to test it
      Message 2 of 5 , Jun 4, 2002
      • 0 Attachment
        I can't reproduce your problem with gcc-3.0.4 or gcc-3.1

        Unzip the enclosed "user" directory to your libs/python directory and run
        bjam there to test it yourself. Works for me.

        -Dave

        ----- Original Message -----
        From: "John Hunter" <jdhunter@...>


        >
        > I have been using boost python for some time now and recently upgraded
        > to boost_1_28_0 and gcc 3.0.4. I started getting a segmentation fault
        > when using a library with an overloaded constructor. In trying to
        > track down the source of the problem, I wrote a simple class with an
        > overloaded constructor and still get the seg fault.



        [Non-text portions of this message have been removed]
      • John Hunter
        Hi David, I have found out a little bit more about the problem. It only occurs if I link with the dynamic library generated by bjam in
        Message 3 of 5 , Jun 6, 2002
        • 0 Attachment
          Hi David,

          I have found out a little bit more about the problem. It only occurs
          if I link with the dynamic library generated by bjam in
          build/bin/libboost_python.so/gcc/release/inlining-on/runtime-link-dynamic/shared-linkable-true/libboost_python.so.1.28.0.
          When I link against the static library, which I built using the
          makefile linux_gcc.mak, the segmentation fault no longer occurs. I
          replicated the problem with OverloadTest in comprehensive.cpp (seg
          fault occurs when calling the first overloaded constructor iff linking
          against the shared lib compiled by bjam). Are you linking against the
          static or dynamic version of libboost_python?

          Below I'll include some relevant lines of my bjam output which I
          compiled with debug 2 and some from the make output where I build the
          static library. Could any of the flags that bjam sets be causing my
          problem if I don't also set them when building my extension module?
          bjam is setting -ftemplate-depth-100 -DBOOST_PYTHON_DYNAMIC_LIB -g -O0
          -fno-inline -fPIC and I am setting -fPIC and -ftemplate-depth-100.

          BTW: I could not find the enclosed user directory you referred to in
          your post.

          Thanks,
          John Hunter

          ----

          Sample bjam out:

          gcc-C++-action libs/python/build/bin/libboost_python.so/gcc/debug/runtime-link-dynamic/shared-linkable-true/errors.o

          g++ -c -Wall -ftemplate-depth-100 -DBOOST_PYTHON_DYNAMIC_LIB -g -O0 -fno-inline -fPIC -I"libs/python/build" -I"/usr/local/include/python2.1" -I"/workn/jdhunter/c/lib/boost_1_28_0" -o "libs/python/build/bin/libboost_python.so/gcc/debug/runtime-link-dynamic/shared-linkable-true/errors.o" "libs/python/build/../src/errors.cpp"

          gcc-Link-action libs/python/build/bin/libboost_python.so/gcc/debug/runtime-link-dynamic/shared-linkable-true/libboost_python.so

          LD_LIBRARY_PATH=.:/home/jdhunter/c/lib:/usr/local/lib:/usr/lib:/lib:/usr/X11R6/lib:/usr/local/lib/mysql/:/home/jdhunter/python/lib:/usr/local/lib/vtk:/home/jdhunter/c/lib/xerces/lib:/usr/local/matlab/extern/lib/glnx86:/usr/local/matlab/sys/os/glnx86/
          export LD_LIBRARY_PATH
          g++ -g -fPIC -shared -o "libs/python/build/bin/libboost_python.so/gcc/debug/runtime-link-dynamic/shared-linkable-true/libboost_python.so.1.28.0" -L/usr/local/lib/python2.1/config "libs/python/build/bin/libboost_python.so/gcc/debug/runtime-link-dynamic/shared-linkable-true/types.o" "libs/python/build/bin/libboost_python.so/gcc/debug/runtime-link-dynamic/shared-linkable-true/classes.o" "libs/python/build/bin/libboost_python.so/gcc/debug/runtime-link-dynamic/shared-linkable-true/conversions.o" "libs/python/build/bin/libboost_python.so/gcc/debug/runtime-link-dynamic/shared-linkable-true/extension_class.o" "libs/python/build/bin/libboost_python.so/gcc/debug/runtime-link-dynamic/shared-linkable-true/functions.o" "libs/python/build/bin/libboost_python.so/gcc/debug/runtime-link-dynamic/shared-linkable-true/init_function.o" "libs/python/build/bin/libboost_python.so/gcc/debug/runtime-link-dynamic/shared-linkable-true/module_builder.o" "libs/python/build/bin/libboost_python.so/gcc/debug/runtime-link-dynamic/shared-linkable-true/objects.o" "libs/python/build/bin/libboost_python.so/gcc/debug/runtime-link-dynamic/shared-linkable-true/cross_module.o" "libs/python/build/bin/libboost_python.so/gcc/debug/runtime-link-dynamic/shared-linkable-true/errors.o" -lutil -Wl,-rpath-link,. "-Wl,-soname,libboost_python.so.1.28.0"
          ln -fs "libboost_python.so.1.28.0" "libs/python/build/bin/libboost_python.so/gcc/debug/runtime-link-dynamic/shared-linkable-true/libboost_python.so"


          ---

          Sample make static lib out:

          g++ -I/usr/local/include/boost -I/usr/local/include/python2.1 -fPIC -ftemplate-depth-21 -g -c errors.cpp
          rm -f libboost_python.a
          ar r libboost_python.a classes.o conversions.o errors.o extension_class.o functions.o init_function.o module_builder.o objects.o types.o cross_module.o


          >>>>> "David" == David Abrahams <david.abrahams@...> writes:

          David> I can't reproduce your problem with gcc-3.0.4 or gcc-3.1
          David> Unzip the enclosed "user" directory to your libs/python
          David> directory and run bjam there to test it yourself. Works for
          David> me.
        • David Abrahams
          ... From: John Hunter ... Sorry, please see the enclosed. Unzip the enclosed user directory to your libs/python directory
          Message 4 of 5 , Jun 6, 2002
          • 0 Attachment
            ----- Original Message -----
            From: "John Hunter" <jdhunter@...>

            > BTW: I could not find the enclosed user directory you referred to in
            > your post.

            Sorry, please see the enclosed.
            Unzip the enclosed "user" directory to your libs/python directory and run
            bjam there to test it yourself. Works for me.
            If you can't make that work, please let me know.


            [Non-text portions of this message have been removed]
          • David Abrahams
            Sorry, it appears this group is configured to ignore attachments. See http://groups.yahoo.com/group/Boost-Users/files/user.zip -Dave ... From: David Abrahams
            Message 5 of 5 , Jun 6, 2002
            • 0 Attachment
              Sorry, it appears this group is configured to ignore attachments. See
              http://groups.yahoo.com/group/Boost-Users/files/user.zip

              -Dave
              ----- Original Message -----
              From: "David Abrahams" <david.abrahams@...>
              Newsgroups: gmane.comp.lib.boost.user
              Sent: Thursday, June 06, 2002 12:34 PM
              Subject: Re: Re: Boost::Python constructor overloading problems


              >
              > ----- Original Message -----
              > From: "John Hunter" <jdhunter@...>
              >
              > > BTW: I could not find the enclosed user directory you referred to in
              > > your post.
              >
              > Sorry, please see the enclosed.
              > Unzip the enclosed "user" directory to your libs/python directory and run
              > bjam there to test it yourself. Works for me.
              > If you can't make that work, please let me know.
              >
              >
              > [Non-text portions of this message have been removed]
              >
              >
            Your message has been successfully submitted and would be delivered to recipients shortly.