Search code examples
c++c++11std-function

Why can't we trivially copy std::function


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. :)


Solution

  • 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.