I need to find a way to intentionally leak (take ownership of) the internal pointer of a std::vector
so that its lifetime surpasses the one of the original container and so that it can be later deleted manually.
Why? I'm working on a networked application using the C ENet library that needs to send large amounts of packets in a short amount of time.
I create network messages by writing the data to a std::vector<unsigned char>
.
Then in order to create a "packet," I use the enet_packet_create
function, which takes a pointer to a byte array to be sent and its size. In normal mode of operation, the function simply dynamically duplicates the given array on the heap, but there is also a "no allocate" option which only takes the pointer and size, leaving deleting to the creator using a callback function, and that's exactly what I'm trying to achieve -- the data is already there in the vector ready to be used, so there is no need to copy it again, as it could be costly.