Boost::bind documentation states:
By default, bind makes a copy of the provided function object. boost::ref and boost::cref can be used to make it store a reference to the function object, rather than a copy.
I am seeing excessive news and deletes in my code when I use boost::bind with a pointer to a member function. The documentation is unclear to me whether boost::bind(&classname::functionname,…) makes a copy of the function.
Or maybe the question makes no sense and as it is converting a member function into a function object it obviously has to new the function object.
So my question is there a way I can bind to a member function and avoid the new and delete?
According to my experiments (boost 1.49),
boost::binddoes not use dynamic memory for its own implementation. With this codeI tried breaking on
operator newvariants in gdb and it didn’t fire. So I suspect your problem is actually thatboost::bindis making copies of either the bound arguments (compare the output of these two snippets) or the call arguments. Search for something among these that could allocate memory while copying, and try to get rid of it.If you’re using a C++11 compiler, you can get away with
boost::bindcompletely and use a lambda function instead. The example would be transformed toThat would only copy if you’d capture objects by value.
EDIT: With the code you posted, try changing the line in question to
That should make it better; report if it doesn’t.