简体   繁体   中英

Memory leak while using shared_ptr

 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

Can anyone help me? I got a problem relating to memory leaks. I don't know how to solve it, can anyone can give some suggestions, it will greatly be appreciated.

Here are some info about my code. I created a struct named ObjectData and a class named ObjectLoader just as follows:

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();
};

Here is the getModel() and ObjectLoader() implementation code:

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());
}

When I test my code I get the problem related to the memory leaks.

Here is my test code:

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

You have a problem detecting the leaks because of the scope of the std::shared_ptr .

In the code;

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

The loader and data destructors, and hence the deletions, do not run until after the _CrtDumpMemoryLeaks(); function reports the leaks.

Adding an extra scope can help with this, else the code needs to be restructured.

{
    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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM