When rust drop runs to free waifu, calling free_waifu2x(), a STATUS_ACCESS_VIOLATION or STATUS_STACK_BUFFER_OVERRUN may occur. After taking a look at this ncnn issue it appears to be the reason why.
In the generated code, we can see that ncnn::Net::~Net() is clearly being run after ncnn::destroy_gpu_instance(), thus causing this issue (of which the link before says that should not happen).
7FF6C1929CFC: E8 6F 82 01 00 callq 0x7ff6c1941f70 ; void ncnn::VulkanDevice::reclaim_blob_allocator(class ncnn::VkAllocator *) const
7FF6C1929D01: 48 8B 53 38 movq 0x38(%rbx), %rdx
7FF6C1929D05: 48 8B 4B 78 movq 0x78(%rbx), %rcx
7FF6C1929D09: E8 5E 84 01 00 callq 0x7ff6c194216c ; void ncnn::VulkanDevice::reclaim_staging_allocator(class ncnn::VkAllocator *) const
7FF6C1929D0E: E8 31 6D 01 00 callq 0x7ff6c1940a44 ; void ncnn::destroy_gpu_instance(void)
7FF6C1929D13: 48 8D 4B 08 leaq 0x8(%rbx), %rcx
7FF6C1929D17: E8 08 8A 02 00 callq 0x7ff6c1952724 ; ncnn::Net::~Net(void)
7FF6C1929D1C: BA A0 00 00 00 movl $0xa0, %edx
After removing ncnn::destroy_gpu_instance(); from ~waifu2x() and placing it at the bottom of free_waifu2x() in order to guarantee the destructor order, I have verified the crash is no longer present (and I have not seen a single crash on exit after this patch). As to why this crash didn't occur in simpler code, I have no clue. But I am using this with a complex program with threading and async, and that seems to have triggered the crashes.
I don't know c++, so I do not know how to edit the code in order to guarantee the destructor order (other than placing ncnn::destroy_gpu_instance() in free_waifu2x()), so I can't offer a PR for this, but I think it should probably be easy to solve?
It might also be worth noting that calling the instance destructor here also appears to have the same problem
this->gpu_count = ncnn::get_gpu_count();
if (gpuid < 0 || gpuid >= gpu_count)
{
fprintf(stderr, "invalid gpu device");
ncnn::destroy_gpu_instance();
return;
}
When rust drop runs to free waifu, calling
free_waifu2x(), aSTATUS_ACCESS_VIOLATIONorSTATUS_STACK_BUFFER_OVERRUNmay occur. After taking a look at this ncnn issue it appears to be the reason why.In the generated code, we can see that
ncnn::Net::~Net()is clearly being run afterncnn::destroy_gpu_instance(), thus causing this issue (of which the link before says that should not happen).After removing
ncnn::destroy_gpu_instance();from~waifu2x()and placing it at the bottom offree_waifu2x()in order to guarantee the destructor order, I have verified the crash is no longer present (and I have not seen a single crash on exit after this patch). As to why this crash didn't occur in simpler code, I have no clue. But I am using this with a complex program with threading and async, and that seems to have triggered the crashes.I don't know c++, so I do not know how to edit the code in order to guarantee the destructor order (other than placing
ncnn::destroy_gpu_instance()infree_waifu2x()), so I can't offer a PR for this, but I think it should probably be easy to solve?It might also be worth noting that calling the instance destructor here also appears to have the same problem