Note that there are some explanatory texts on larger screens.

plurals
  1. POboost::bind internal copy/copies?
    primarykey
    data
    text
    <p>I was hoping to understand what kind of internal copies of a function object does boost::bind do. Since constructors of these objects do not seem to be invoked, I presumed this is a kind of "very shallow copy", so I introduced dynamic memory allocations to produce some errors. However, runtime output of the code below seems to indicate <em>three</em> additional destructor calls for internal copies made by bind.</p> <pre><code>using namespace std; using namespace boost; class M{ int *somedata; public: M(){ somedata=new int[5]; cout&lt;&lt;"from cstr\n"; somedata[1]=0;} ~M(){cout&lt;&lt;"from dstr\n"; delete somedata;} int operator()(int i){ cout&lt;&lt;++somedata[i]&lt;&lt;endl; return 0;} }; int main() { M instM; bind&lt;int&gt;(instM,1)(); //bind&lt;int&gt;(&amp;M::operator(),&amp;instM,1)(); //this works with no errors, of course instM(1); //would not change the order of output return 0; } </code></pre> <p>The output ... presents some additional puzzles - eg. why first dstr event comes before call to operator()? Also notice the "2" before the last failed destructor call. </p> <pre><code>from cstr from dstr 1 from dstr bind_copy(73365) malloc: *** error for object 0x1001b0: double free *** set a breakpoint in malloc_error_break to debug from dstr bind_copy(73365) malloc: *** error for object 0x1001b0: double free *** set a breakpoint in malloc_error_break to debug 2 from dstr bind_copy(73365) malloc: *** error for object 0x1001b0: double free *** set a breakpoint in malloc_error_break to debug </code></pre> <p>So the question is: can anyone explain briefly in what order, and what kind of copies does bind make?</p> <p>... After some thought I realized bind is simply using the (here default) copy constructor. After supplying some custom version of this cstr (with memory allocation and as-deep-as-one-wishes version of the copy) the output gets clean (as it should), but the puzzle remains: there are <em>three</em> invocations of the copy constructor. <strong>So in this case boost::bind makes <em>three</em> copies of the function object. Why and in which order?</strong> (For nested boost::binds this can lead to quite explosive growth of the number of internal copies.)</p> <p>Output with cp-cstr defined, and some "heritage markers" added ("P"=parent, each cp cstr adds "-C"):</p> <pre><code> from cstr P from cp cstr P-C from cp cstr P-C-C from cp cstr P-C-C-C from dstr P-C-C P-C-C-C:1 from dstr P-C-C-C from dstr P-C P:1 from dstr P </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload