Ticket #4325 (closed Bugs: fixed)

Opened 7 years ago

Last modified 6 years ago

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

Reported by: Scott French <scott@…> Owned by: dgregor
Milestone: Boost 1.43.0 Component: function
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.


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

Change History

Changed 7 years ago by Scott French <scott@…>

Reproduction example

comment:1 Changed 7 years ago by steven_watanabe

  • Status changed from new to closed
  • Resolution set to fixed

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

comment:2 Changed 6 years ago by danieljames

(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.

Add a comment

Modify Ticket

Change Properties
<Author field>
as closed
The resolution will be deleted. Next status will be 'reopened'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.