全局结构向量的内存泄漏

我正在为使用DirectX的视频游戏开发模块。

为了读取一些游戏数据,我在渲染框架之前挂接了一个称为“函数”的函数,然后在其中收集我的数据并将其推送到全局变量。我从Directx的Present中读取了该向量,该向量在另一个线程上。

// Globals
struct SoldierLogicData {
    float health;
    float maxHealth;
    bool vehicle;
    float healthVehicle;    
    float maxHealthVehicle;
};
std::vector<SoldierLogicData> players;

这是我从渲染功能更新此向量的地方

void hkPreFrameRender() {
    players.clear();

    // Collecting data here

    players.push_back({ // <-- This is where the leak is
        soldier->m_Health,
        soldier->m_MaxHealth,
        vehicle ? true : false,
        healthVehicle,
        maxHealthVehicle,
    });
}

我还从另一个线程(directx的Present线程)访问此向量,而没有先正确锁定它:

HRESULT hkPresent(IDXGISwapChain* pThis, UINT SyncInterval, UINT Flags) {
    // Accessing players here

    return oPresent(pThis, SyncInterval, Flags);
}

我知道我在这里做很多坏事,但我真的很好奇为什么会泄漏?

我尝试使用MSDN上的指南查找泄漏,并且确实在WinDbg中获得以下输出:

Detected memory leaks!
Dumping objects ->
{174} normal block at 0x00000000FE227A60, 102 bytes long.
 Data: <       B  HB5IW > 01 CC CC CC 00 00 C8 42 00 00 48 42 35 49 57 BF 
{163} normal block at 0x00000002306EA3C0, 16 bytes long.
 Data: <0}1             > 30 7D 31 87 F9 7F 00 00 00 00 00 00 00 00 00 00 
Object dump complete.

102 is exactly the size of 6 SoldierLogicData and I indeed got exactly 6 players on the map when getting that output

评论