user800799
user800799

Reputation: 3023

How do I delete map instance?

map<long long, vector<SoundInfo *> > DataCenter::getCopySoundListMap(){

    lock();

    map<long long, vector<SoundInfo *> > copyMap(m_soundListMap);

    unLock();

    return copyMap;
}

I have this method. It makes a copy of map and returns the copy.

I am calling this method like this

map<long long, vector<SoundInfo *> > copyMap = DataCenter::getInstance()->getCopySoundListMap();

After I am done with it at the end of the function, I did something like this

delete copyMap;

And it complains that this is not a pointer.. I know that this is not a pointer then I was wondering when the instance of the copied map is going to be released.

Thanks in advance...

Upvotes: 0

Views: 159

Answers (1)

Tony Delroy
Tony Delroy

Reputation: 106246

It's a local variable, so it gets released when it goes out of scope... that is, when the {...} block it's inside reaches that }. It doesn't matter whether it's a function scope, a for or while loop, just an arbitrarily blocked group of statements... the variables introduced inside that block have their destructors invoked at the end. The memory for the map object itself comes from the stack, and is reclaimed for use by further variables, perhaps locals from other functions that are called. That map object - while non-empty - will have pointers into heap memory as well, they'll be released during destruction.

Upvotes: 2

Related Questions