为什么这个shared_ptr在超出范围时会抛出一个断言?

时间:2021-10-02 01:57:01

Why does the following code trigger an assert? This code initially worked and at some point started triggering an assert as the shared_ptr fell out of scope.

为什么以下代码触发断言?这段代码最初起作用,并且在某些时候因shared_ptr超出范围而开始触发断言。

#include <iostream>
#include <memory>
#include "GLFW/glfw3.h"
int main()
{
    if (!glfwInit()){
        std::cout << "Failed to initialize GLFW." << std::endl;
        return -1;
    }
    auto window = std::shared_ptr<GLFWwindow>(glfwCreateWindow(1024, 768, "Test", NULL, NULL));
    return 0;
}

I've taken just the minimum amount of code that I can to reproduce this, and maybe I misunderstand the use of shared_ptr. I've also tried it with the syntax of:

我只使用了最少量的代码来重现这一点,也许我误解了shared_ptr的用法。我也试过它的语法:

std::shared_ptr<GLFWwindow> window(glfwCreateWindow(1024, 768, "Test", NULL, NULL));

The exact error message I am getting in the output window of the debugger (VS2013) is as follows:

我在调试器(VS2013)的输出窗口中得到的确切错误消息如下:

Debug Assertion Failed!

Program: C:\Users\...\xxxx.exe
File: f:\dd\vctools\crt\crtw32\misc\dbgdel.cpp
Line: 52

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

From what I've researched it seems that it is trying to free the shared_ptr twice - is this the case and how can I prevent that? It may be worth mentioning that swapping the type from GLFWwindow to struct test { int i; }; no longer triggers the assert. Does this mean that GLFWwindow is deleting the pointer internally? If so why did the code work at one point but not now?

从我研究的内容来看,它似乎试图将shared_ptr两次释放 - 是这种情况,我该如何防止这种情况?值得一提的是,将类型从GLFWwindow交换到struct test {int i; };不再触发断言。这是否意味着GLFWwindow在内部删除指针?如果是这样,为什么代码在某一点工作但现在不工作?

1 个解决方案

#1


6  

Most likely because glfwCreateWindow allocates the data using malloc, and std::shared_pointer free the memory using delete. Those are two different memory allocation systems, and should not be mixed.

很可能是因为glfwCreateWindow使用malloc分配数据,而std :: shared_pointer使用delete释放内存。这是两种不同的内存分配系统,不应混用。

Besides, you can't just free the pointer returned by glfwCreateWindow, you need to close the window properly, because you have no idea what other data may have been allocated by glfwCreateWindow. You need a custom delete that calls glfwDestroyWindow.

此外,你不能只释放glfwCreateWindow返回的指针,你需要正确关闭窗口,因为你不知道glfwCreateWindow可能分配了哪些其他数据。您需要一个调用glfwDestroyWindow的自定义删除。

#1


6  

Most likely because glfwCreateWindow allocates the data using malloc, and std::shared_pointer free the memory using delete. Those are two different memory allocation systems, and should not be mixed.

很可能是因为glfwCreateWindow使用malloc分配数据,而std :: shared_pointer使用delete释放内存。这是两种不同的内存分配系统,不应混用。

Besides, you can't just free the pointer returned by glfwCreateWindow, you need to close the window properly, because you have no idea what other data may have been allocated by glfwCreateWindow. You need a custom delete that calls glfwDestroyWindow.

此外,你不能只释放glfwCreateWindow返回的指针,你需要正确关闭窗口,因为你不知道glfwCreateWindow可能分配了哪些其他数据。您需要一个调用glfwDestroyWindow的自定义删除。