• Advertisement
Sign in to follow this  

global std::vector causes memory leaks?

This topic is 2921 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When I was trying to track down a rather strange memory leak, I traced it to a static vector<int>. I found that the problem could also be duplicated using a global variable as well, as follows:
#include <vector>

vector<int> myVec(5);

int main()
{
  
  return 0;
}

Why exactly is this an automatic memory leak? Shouldn't it's dynamic array be deleted when the destructor is called?

Share this post


Link to post
Share on other sites
Advertisement
Yes, but the destructor is not called until after main has exited, therefore many leak detectors will give a false-positive (because they run just before main exits, or perhaps they also run after main but the order is non-deterministic [that is, they may run before the destructor is called, or they may run after]).

What are you using to detect the leak.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement