Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#4325 closed Bugs (fixed)

boost::function target method fails after copy constructing a function containing a ref

Reported by: Scott French <scott@…> Owned by: Douglas Gregor
Milestone: Boost 1.43.0 Component: function
Version: Boost Development Trunk Severity: Regression
Keywords: function ref copy constructor Cc:

Description

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.

Attachments (1)

test_function.cpp (336 bytes ) - added by Scott French <scott@…> 12 years ago.
Reproduction example

Download all attachments as: .zip

Change History (3)

by Scott French <scott@…>, 12 years ago

Attachment: test_function.cpp added

Reproduction example

comment:1 by Steven Watanabe, 12 years ago

Resolution: fixed
Status: newclosed

(In [62665]) Make sure that the cv flags are copied when we copy a reference to a function object. Fixes #4325

comment:2 by Daniel James, 12 years ago

(In [70361]) Function: Merge from trunk.

  • Remove extra definition of operator(), since it's inline anyway. Fixes #4765.
  • Make sure that the cv flags are copied when we copy a reference to a function object. Fixes #4325
  • Fully qualified function calls to avoid ambiguity with new additions to standard. Thanks to Conrad Poelman. Fixes #4073
  • Remove iterator workaround for newer versions of Visual Age C++. Thanks to 'ccambly'. Fixes #3912
  • Fix unused variable warning for Visual C++. Fixes #3618
  • Testing flag for Visual C++.
  • Removed all but one old-style cast, prevents GCC warnings, but breaks GCC 2.95.3. Fixes #3410
  • Fixed tab and newline issues from inspection report.
Note: See TracTickets for help on using tickets.