c ++向量获取方法returns nullptr在Release但不是Debug

c++ Vector fetch method returns nullptr in Release but not Debug

我目前有一个资源管理类型系统,它在启动时从该系统加载一次对象,并在应用程序中加载其他对象 "fetch"。在调试版本中这完美地工作,但在发布版本中它 returns 空指针。例如,这是资源管理器的一个部分:

// Add a single mesh to the manager
bool Soft351ResourceManager::AddMesh(
    ID3D11Device* pd3Device,
    LPCTSTR meshFile,               // mesh file location   
    bool createAdjacentIndicies,
    LPCTSTR meshName,               // Unique name
    Soft351ShaderAmbient* shader            // shader for the mesh to use
)
{
    bool result;
    Soft351Mesh mesh;

    // attempt to load the mesh file
    result = mesh.Setup(
        pd3Device,
        meshFile,
        createAdjacentIndicies,
        meshName,
        shader
    );
    // if failed to load return false
    if (!result) {
        return false;
    }

    // add object to the mesh list
    m_meshList.push_back(mesh);

    return true;
}

// Get mesh by unique name
Soft351Mesh* Soft351ResourceManager::GetMesh(LPCTSTR name)
{
    // Loop through list and check the name, if match return the object
    for (int i = 0; i < m_meshList.size(); i++) {
        if (m_meshList[i].GetName() == name) {
            return &m_meshList[i];
        }
    }

    // return null
    return 0;
}

这就是错误发生的地方:

// --- Game objects:
// Create the player
g_player = new Soft351PlayerObject();
// Give the player a mesh from the asset manager
g_player->mesh = g_resources.GetMesh(L"player");
// give the shield pickup a mesh from the asset manager
g_player->meshShield = g_resources.GetMesh(L"shield");
// Object has alpha based texture
g_player->meshShield->alphaTexture = true;

发生在 g_player->meshShield->alphaTexture = true,错误为 Exception thrown: write access violation. g_player->meshShield was nullptr.

资源加载有错误检查,没有错误或消息出现,visual studio表明资源管理向量已填充数据。但是每次在发布模式下访问它都会导致 nullptr 或类似的错误。可能是什么原因造成的?

而不是

m_meshList[i].GetName() == name

你应该做

lstrcmp(m_meshList[i].GetName(), name) == 0)

目前您正在比较在调试和发布版本中显然不同的指针