2014-02-10 36 views
0

看起來的std :: vector.resize實際上並不有時分配內存。如果我運行這段代碼:的std :: vector.resize有時不分配內存?

std::vector<InstanceData> instanceData; 
instanceData.resize(meshCount); 

float randX = 0.0f; 
float randY = 0.0f; 
float randZ = 0.0f; 

float maxX = 1000.0f; 
float maxY = 1000.0f; 
float maxZ = 1000.0f; 

for(int i = 0; i < meshCount; i++) 
{ 
    randX = (rand() % 100) * 0.01f; 
    randY = (rand() % 100) * 0.01f; 
    randZ = (rand() % 100) * 0.01f; 

    instanceData[i].worldMatrix = DirectX::XMMatrixTranspose(DirectX::XMMatrixTranslation(randX * maxX, randY * maxY, randZ * maxZ)); 
    instanceData[i].color = DirectX::XMFLOAT4(randX, randY, randZ, 1.0f); 
} 

程序,當我建立了釋放,但貌似不是我建的調試有時會崩潰。我使用Visual Studio 2013進行編譯。

如果我將resize(meshCount)更改爲reserve(meshCount)(或者只是刪除它)並訴諸使用臨時InstanceData,然後我只是填充數據並將其推入向量中,看起來不會崩潰。

如果我將調試器附加到發行版本,它給了我此錯誤消息:在path.exe在0x00BB5BCB

未處理的異常:0000005:訪問衝突讀取位置00000000。

任何想法是怎麼回事?爲什麼我不能使用resize

編輯: 這裏的InstanceData

struct InstanceData 
{ 
    DirectX::XMMATRIX worldMatrix; 
    DirectX::XMFLOAT4 color; 
}; 
+1

*您可以使用*調整大小XMFLOAT4X4。問題很可能在其他地方。你應該發佈一個簡單的測試用例來重現問題。 – juanchopanza

+0

你能分享InstanceData的定義是什麼? – Pedrom

+0

'resize'和'reserve'做2個不同(雖然相關的)東西...... –

回答

0

解決方案是使用,而不是XMMATRIX