2016-07-18 84 views
0
Detected memory leaks! 
Dumping objects -> 
{9370} normal block at 0x000000C16B24C480, 24 bytes long. 
Data: <`h=    > 60 68 3D FB F6 7F 00 00 01 00 00 00 01 00 00 00 
{8549} normal block at 0x000000C16B25CC30, 21627 bytes long. 
Data: <  0 %k > FA FA FA FA FA FA FA FA 30 CC 25 6B C1 00 00 00 
{5196} normal block at 0x000000C16B253320, 12839 bytes long. 
Data: <    > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
{192} normal block at 0x000000C16B24CE40, 24 bytes long. 
Data: < m=    > 20 6D 3D FB F6 7F 00 00 02 00 00 00 01 00 00 00 
{191} normal block at 0x000000C16B251780, 16 bytes long. 
Data: < $k   > 10 DB 24 6B C1 00 00 00 00 00 00 00 00 00 00 00 
{190} normal block at 0x000000C16B251410, 16 bytes long. 
Data: < $k   > F0 DA 24 6B C1 00 00 00 00 00 00 00 00 00 00 00 
{189} normal block at 0x000000C16B2514B0, 16 bytes long. 
Data: < $k   > D0 DA 24 6B C1 00 00 00 00 00 00 00 00 00 00 00 
{188} normal block at 0x000000C16B2516E0, 16 bytes long. 
Data: < $k   > B0 DA 24 6B C1 00 00 00 00 00 00 00 00 00 00 00 
{187} normal block at 0x000000C16B251690, 16 bytes long. 
Data: < $k   > 90 DA 24 6B C1 00 00 00 00 00 00 00 00 00 00 00 
{186} normal block at 0x000000C16B251370, 16 bytes long. 
Data: <p $k   > 70 DA 24 6B C1 00 00 00 00 00 00 00 00 00 00 00 
{185} normal block at 0x000000C16B251230, 16 bytes long. 
Data: <P $k   > 50 DA 24 6B C1 00 00 00 00 00 00 00 00 00 00 00 
{184} normal block at 0x000000C16B24DA50, 224 bytes long. 
Data: <0 %k @3%k > 30 12 25 6B C1 00 00 00 40 33 25 6B C1 00 00 00 
{156} normal block at 0x000000C16B24C4E0, 24 bytes long. 
Data: <P $k @ $k > 50 DA 24 6B C1 00 00 00 40 CE 24 6B C1 00 00 00 
{155} normal block at 0x000000C16B24C300, 32 bytes long. 
Data: <../dataset/refer> 2E 2E 2F 64 61 74 61 73 65 74 2F 72 65 66 65 72 
{154} normal block at 0x000000C16B250AB0, 16 bytes long. 
Data: < k   > A8 F4 09 6B C1 00 00 00 00 00 00 00 00 00 00 00 
Object dump complete. 
'3DMM_1st.exe' (Win32): Loaded 'C:\Windows\System32\kernel.appcore.dll'. Cannot find or open the PDB file. 
The program '[36392] 3DMM_1st.exe' has exited with code 1 (0x1).strong text 

任何人都可以幫我嗎?我遇到了與內存泄漏有關的問題。我不知道如何解決它,任何人都可以提出一些建議,將不勝感激。使用shared_ptr時發生內存泄露

這是關於我的代碼的一些信息。我創建了一個struct名爲ObjectDataclass命名ObjectLoader剛剛如下:

struct ObjectData { 
    std::vector <glm::vec3> vertices, normals, colors; 
    std::vector <glm::vec2> texCoords; 
    std::vector <unsigned int> vIndices, uIndices, nIndices; 
}; 

class ObjectLoader { 
    private: 
    std::tr1::shared_ptr<ObjectData> object; 
    bool hasUV, hasNormals, hasColor, colorChecked, indexChecked; 

    std::string parseString(std::string src, std::string code); 
    std::vector<glm::vec3> parseVerColor(std::string src, std::string code); 
    glm::vec2 parseVec2(std::string src, std::string code); 
    glm::vec3 parseVec3(std::string src, std::string code); 
    void addIndices(std::string str); 
    void checkIndices(std::string str); 
    void checkColors(std::string str); 
    void loadObjects(std::string objPath); 

    public: 
    ObjectLoader(std::string objName); 
    ~ObjectLoader(); 

    std::tr1::shared_ptr<ObjectData> getModel(); 
}; 

這裏是getModel()ObjectLoader()實現代碼:

std::tr1::shared_ptr<ObjectData> ObjectLoader::getModel() { 
    return object; 
} 

ObjectLoader::ObjectLoader(std::string objName) { 
    indexChecked = false; 
    colorChecked = false; 
    std::string fileName = objName; 
    object = std::tr1::shared_ptr<ObjectData>(new ObjectData()); 
} 

當我測試的代碼我得到相關的問題內存泄漏。

這裏是我的測試代碼:

std::tr1::shared_ptr<ObjectLoader> loader = std::tr1::shared_ptr<ObjectLoader>(new ObjectLoader(fileName)); 
    std::tr1::shared_ptr<ObjectData> data = loader->getModel(); 
    _CrtDumpMemoryLeaks(); 
+2

如何爲'的std :: TR1 :: shared_ptr的 getModel();'定義? ObjectLoader(std :: string objName);'做什麼? – NathanOliver

+0

getModel()返回shared_ptr對象。在ObjectLoader中我創建了object = std :: tr1 :: shared_ptr (new ObjectData()); –

+0

請將[編輯]到你的問題中,以便它清晰可辨。 – NathanOliver

回答

3

你有問題,因爲檢測的std::shared_ptr範圍的泄漏。

在代碼中;

std::tr1::shared_ptr<ObjectLoader> loader = std::tr1::shared_ptr<ObjectLoader>(new ObjectLoader(fileName)); 
std::tr1::shared_ptr<ObjectData> data = loader->getModel(); 
_CrtDumpMemoryLeaks(); 

loaderdata析構函數,因此缺失,不運行,直到後_CrtDumpMemoryLeaks();功能報告泄漏。

添加一個額外的範圍可以幫助這個,否則代碼需要重新構造。

{ 
    std::tr1::shared_ptr<ObjectLoader> loader = std::tr1::shared_ptr<ObjectLoader>(new ObjectLoader(fileName)); 
    std::tr1::shared_ptr<ObjectData> data = loader->getModel(); 
} // destructors run here... 
_CrtDumpMemoryLeaks(); 
+1

謝謝!當我添加額外範圍到我的代碼時,我發現內存泄漏錯誤消失了。 –

相關問題