samedi 28 novembre 2015

c++ deleting static objects

I am just learning C++, and I'm having some trouble with memory leaks. I am using Visual Studio express, and I have enabled the crtdbg commands to dump memory leaks when the program closes. I cannot for the life of me, though, see why this particular line is being marked as a memory leak.

I have a class for Resource, and it contains a

static std::unordered_map< std::string, std::unique_ptr < Resource >> RESOURCE_LIBRARY;

This contains the definitions of the available resources, and then a factory method makes new resources based on these definitions when new resources are needed.

I then have a method that populates this map like this:

std::unique_ptr< Resource > lResource = std::unique_ptr< Resource >(new Resource(0, 0));
Resource::RESOURCE_LIBRARY["blue"] = std::move(lResource);

The problem I am seeing is that these lines to populate the map are the source of the memory allocation that isn't cleared up, according to Visual Studio. I have tried a method to free this like this:

for (auto it = RESOURCE_SOURCE_LIBRARY.begin(); it != RESOURCE_SOURCE_LIBRARY.end(); it++)
{
    it->second.reset();
}

but I still get the same messages out. I am sure that I must be missing/misunderstanding something here, so any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire