I'm trying to wrap VkInstance (opaque pointer) into unique_ptr
but seems like I can't.
...
VkInstance instance;
if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
throw std::runtime_error("failed to create vulkan instance");
}
auto del = [](VkInstance* p) {
DBG("release vk instance");
vkDestroyInstance(*p, nullptr);
};
auto ptr = std::unique_ptr<VkInstance, decltype(del)>(instance, del);
...
The error:
no instance of constructor "std::unique_ptr<_Tp, _Dp>::unique_ptr [with _Tp=VkInstance, _Dp=lambda [](VkInstance *p)->void]" matches the argument list
I can't figure out why. VkInstance is a pointer, so I'm passing it, deleter have to accept pointer to a memory address, so it receiving it still, but still types doesn't match.
Dereferrencing it with &
and passing it into make_unique causes a segfault. Makes sense.
The only way I managed to make it work only with with additional new call, like this:
...
VkInstance* instance = new VkInstance;
if (vkCreateInstance(&createInfo, nullptr, instance) != VK_SUCCESS) {
throw std::runtime_error("failed to create vulkan instance");
}
auto del = [](VkInstance* p) {
DBG("release vk instance");
vkDestroyInstance(*p, nullptr);
};
auto ptr = std::unique_ptr<VkInstance, decltype(del)>(instance, del);
...
But this is kinda ridiculous solution, as I'm allocation dynamically for a thing which should lay in a CPU register and transferred almost immediately to unique_ptr
area of control.
So, can I achieve somehow what I'm trying to do, without further overengineering?