Skip to main content
GameDev.net gamedev.net
🔒 Locked

global std::vector causes memory leaks?

Started by generaleskimo Feb 24, 2010 at 11:34 PM 1 replies 1.4k views
Original Post
generaleskimo
generaleskimo
When I was trying to track down a rather strange memory leak, I traced it to a static vector. 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?
===========================";" is the best key ever; you can use it to end lines when you are too lazy to use ENTER;
Codeka
Codeka
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.
generaleskimo
generaleskimo
I found the leak using MemWatch, which makes a lot of sense in retrospect.
===========================";" is the best key ever; you can use it to end lines when you are too lazy to use ENTER;

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.