Sign in to follow this  

global std::vector causes memory leaks?

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this