使用shared_ptr时内存泄漏

时间:2016-07-18 12:52:03

标签: c++ memory-leaks

 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

任何人都可以帮助我吗?我遇到了与内存泄漏有关的问题。我不知道如何解决它,任何人都可以提出一些建议,我们将不胜感激。

以下是有关我的代码的一些信息。我创建了一个名为struct的{​​{1}}和一个名为ObjectData的{​​{1}},如下所示:

class

以下是ObjectLoaderstruct 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()

1 个答案:

答案 0 :(得分: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();