Opened 6 years ago

Closed 6 years ago

#12275 closed Bugs (fixed)

bind fwds shared_ptr twice, invokes memfn with nullptr

Reported by: Jason Mancini <jayrusman@…> Owned by: Peter Dimov
Milestone: To Be Determined Component: bind
Version: Boost 1.61.0 Severity: Problem
Keywords: Cc:

Description

Behavior started in 1.60, due to bind/bind.hpp changes around forwarding and rvalues. Short sample is shown below. This limitation needs to be mentioned in the boost docs, or somehow alter bind to work like before. Bind 1.59 would print the ptr address twice. Bind 1.60 gets a nullptr on the second memfn call. Quick fix for this sample is to change tFunc to use shared_ptr<...>const& such that it can't be forwarded.

#include <stdio.h>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>

using namespace boost;
using tFunc = function<bool(shared_ptr<const Obj>)>;

struct Obj { bool MF() const { printf("this %p\n", this); return true; } };

int main() {
  shared_ptr<const Obj> sptr = make_shared<Obj>();
  tFunc BB;
  BB = bind(&Obj::MF, _1);
  BB = bind(BB, _1) && bind(&Obj::MF, _1);
  BB(sptr);
}

Boost 1.59

this 0x117fc39
this 0x117fc39

Boost 1.60

this 0x117fc39
this (nil)

Change History (5)

comment:1 by Jason Mancini <jayrusman@…>, 6 years ago

Let's try that code sample again. :) Slightly adjusted for clarity.

#include <stdio.h>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>

using namespace boost;
struct Obj { bool MF() const { printf("this %p\n", this); return true; } };
using tFunc = function<bool(shared_ptr<const Obj>)>;

int main() {
  shared_ptr<const Obj> sptr = make_shared<Obj>();
  tFunc AA, BB;
  AA = bind(&Obj::MF, _1);
  BB = bind(AA, _1) && bind(&Obj::MF, _1);
  BB(sptr);

comment:2 by Jason Mancini <jayrusman@…>, 6 years ago

}

comment:4 by Jason Mancini <jayrusman@…>, 6 years ago

Wow quick turn-around! Confirmed fix works for both the sample and the actual application. Thanks!

comment:5 by Peter Dimov, 6 years ago

Resolution: fixed
Status: newclosed

Merged to master.

Note: See TracTickets for help on using tickets.