The reason for me to ask this is I need to store std::function
in a vector, and the in-house vector we have in company basically is doing realloc if it needs more memory. (Basically just memcpy
, no copy/move operator involves)
This means all the element we can put in our container need to be trivially-copyable.
Here is some code to demonstrate the problematic copy I had:
void* func1Buffer = malloc(sizeof(std::function<void(int)>));
std::function<void(int)>* func1p = new (func1Buffer) std::function<void(int)>();
std::function<void(int)>* func2p = nullptr;
*func1p = [](int) {};
char func2Buffer[sizeof(*func1p)];
memcpy(&func2Buffer, func1p, sizeof(*func1p));
func2p = (std::function<void(int)>*)(func2Buffer);
// func2p is still valid here
(*func2p)(10);
free(func1Buffer);
// func2p is now invalid, even without std::function<void(int)> desctructor get triggered
(*func2p)(10);
I understand we should support copy/move of the element in order to store std::function
safely.
But I am still very curious about what is the direct cause of invalid std::function
copy above.
----------------------------------------------------UpdateLine----------------------------------------------------
Updated the code sample.
I have found the direct reason for this failure, by debugging our in-house vector more.
The trivially copied std::function
has some dependency on original object memory, delete the original memory will trash the badly copied std::function
even without the destruction of the original object.
Thanks for everyone's answer to this post. It's all valuable input. :)
You can recover the desired behavior by always using thread-local copies of the std::function because they'll each have an isolated copy of the state variables.
std::function can hold function objects (including lambdas), as well as function pointers with the correct signature. So it is more versatile.
If it is small, like 3-5 CPU instructions then yes std::function will make it slower, because std::function is not inlined into outer calling code. You should use only lambda and pass lambda as template parameter to other functions, lambdas are inlined into calling code.
The problem is how std::function
has to be implemented: it has to manage the lifetime of whatever object it's holding onto. So when you write:
{
std::function<Sig> f = X{};
}
we must invoke the destructor of X
when f
goes out of scope. Moreover, std::function
will [potentially] allocate memory to hold that X
so the destructor of f
must also [potentially] free that memory.
Now consider what happens when we try to do:
char buffer[100000]; // something big
{
std::function<void()> f = X{};
memcpy(buffer, &f, sizeof(f));
}
(*reinterpret_cast<std::function<void()>*>(buffer))();
At the point we're calling the function "stored" at buffer
, the X
object has already been destroyed and the memory holding it has been [potentially] freed. Regardless of whether X
were TriviallyCopyable
, we don't have an X
anymore. We have the artist formerly known as an X
.
Because it's incumbent upon std::function
to manage its own objects, it cannot be TriviallyCopyable
even if we added the requirement that all callables it managed were TriviallyCopyable
.
To work in your realloc_vector
, you need either need something like function_ref
(or std::function<>*
) (that is, a type that simply doesn't own any resources), or you need to implement your own version of function
that (a) keeps its own storage as a member to avoid allocating memory and (b) is only constructible with TriviallyCopyable
callables so that it itself becomes trivially copyable. Whichever solution is better depends on the what your program is actually doing.
But I am still very curious about what is the direct cause of invalid std::function copy above.
std::function
cannot be TriviallyCopyable
(or conditionally TriviallyCopyable
) because as a generic callable object wrapper it cannot assume that the stored callable is TriviallyCopyable
.
Consider implementing your own version of std::function
that only supports TriviallyCopyable
callable objects (using a fixed buffer for storage), or use a vector of function pointers if applicable in your situation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With