[C++-sig] Problems with Boost Python scope and -fno-elide-constructors

Martin Reddy reddy at pixar.com
Fri Aug 25 18:31:53 CEST 2006


Hi there,

We recently came across an interesting problem where boost scopes do
not appear to be closed correctly when the code is compiled with the 
-fno-elide-constructors gcc option. We have worked around this with
a small modification to the boost/python/scope.hpp.

For example, take the following simple example:

void
wrapScope1()
{
     scope errorScope =
     class_<Scope1, Scope1*, boost::noncopyable>
         ( "Scope1", "", no_init )
         .def("sayHello", &SayHello);
}

void
wrapScope2()
{
     class_<Scope2, Scope2*, boost::noncopyable>
         ( "Scope2", "", no_init )
         .def("sayHello", &SayHello);
}

BOOST_PYTHON_MODULE( MyModule )
{
     wrapScope1();
     wrapScope2();
}

When compiled normally you get Scope1 and Scope2 at the same level, as you 
would expect. However, if you compile this code with -fno-elide-constructors 
(which we do to workaround gcov bugs) then Scope2 is nested inside of Scope1.

The problem is that -fno-elide-constructors causes 2 scope objects to get 
created: an initial temporary scope object is created, and then the copy 
constructor is called to copy this into the errorScope variable. The 
temporary scope is then immediately destroyed (popping the scope to the 
previous value), and then at the end of wrapScope1() errorScope is destroyed, 
popping the scope to the value of the temporary scope. To work around this, I 
made a small fix to scope.hpp to replace the m_previous_scope pointer with a 
vector that forms a stack of scope pointers. There may be more elegant fixes 
to this problem that this group may want to consider, but I figured I'd pass 
along the one potential change that works well for us.

I have put a complete repro case, along with the code changes that I made to 
address this, and a diff -c patch file, into a tarball at:

   http://www.martinreddy.net/tmp/boost-scope.tgz

(This problem occurred for us under gcc 4.0.x, boost 1.33.1, under Fedora 
Core.)

Cheers,

Martin.


---------------------------------------------------------------------------
Martin Reddy                                        Pixar Animation Studios
                                                     1200 Park Avenue
reddy at pixar.com                                     Emeryville, CA 94608
http://MartinReddy.net/                             Tel. (510) 752 4093



More information about the Cplusplus-sig mailing list