Ticket #4325 (closed Bugs: fixed)
boost::function target method fails after copy constructing a function containing a ref
|Reported by:||Scott French <scott@…>||Owned by:||dgregor|
|Version:||Boost Development Trunk||Severity:||Regression|
|Keywords:||function ref copy constructor||Cc:|
Attached is a simple reproduction case that creates a boost::function containing a boost::ref, copy constructs a second one from the first, and then attempts to get the target object from the second function object. Using g++ 4.2.1 on Mac OS X 10.6.3, this prints out a valid pointer if compiled -O0, but prints "0" (NULL) if compiled -O2. I have done some debugging and believe the problem lies in the function boost::detail::function::reference_manager::manage (line 201 of function_base.hpp in Boost 1.43.0). Each tag case in this function copies only the obj_ptr piece of the obj_ref field. In the case of the boost::function copy constructor, which eventually calls the manage function with clone_functor_tag, this leaves the is_const_qualified and is_volatile_qualified fields of the constructed object uninitialized, leading to sporadic results. Also, I was not able to reproduce this on Linux, and I believe that is because on Linux, the has_trivial_copy_and_destroy method reports true, which causes the manage method to be bypassed, but on Mac OS X, it reports false. I was able to get my test case to pass by changing
out_buffer.obj_ref.obj_ptr = in_buffer.obj_ref.obj_ptr;
in the clone_functor_tag case to
out_buffer.obj_ref = in_buffer.obj_ref;
but I suspect similar changes need to be made for the other tags as well.
Also note that this affects operator== and operator!=, which are implemented in terms of target.