6

Why does valgrind's DRD tool complaines "Conflicting load by thread ... at size 4": about such code:

void SomeFunction(const int& value)
{
    boost::bind(..., value); /* <-- complaines on this line
                                with last backtrace function "new(int)" */
}

Does boost::bind() stores values by reference or value?

Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85
Slaus
  • 2,086
  • 4
  • 26
  • 41
  • if it fails _on the line of the bind_ it is obviously a problem at bind time, so whether bind sotres a copy or a reference is irrelevant then – sehe Jun 18 '11 at 15:53

1 Answers1

14

By value. 1

But you can make it copy by ref instead:

void SomeFunction(const int& value)
{
    boost::bind(..., boost::ref(value)); 
    boost::bind(..., boost::cref(value)); // by const ref
}

1 http://www.boost.org/doc/libs/1_46_1/libs/bind/bind.html#Purpose

a copy of the value of i is stored into the function object. boost::ref and boost::cref can be used to make the function object store a reference to an object, rather than a copy: int i = 5;

bind(f, ref(i), _1);

bind(f, cref(42), _1);

sehe
  • 374,641
  • 47
  • 450
  • 633
  • So why then DRD complaines about "new(int)"? – Slaus Jun 18 '11 at 15:51
  • 2
    @Slav: How am I to know? Post more code so I can run it and see for myself :) I'm happy to help, but it's not enough code. Not by a mile – sehe Jun 18 '11 at 15:52