3

I'm seeing I'm getting a memory leak from my vector I have, I've tried deleting the contents then clearing the vector, erasing the vector as well. My Crtdb is still informing me of memory leaks, I know it involves the vector because when i comment all vector related things i get no leaks. Here is all my code is doing.

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <vector>
#include "MyClass.h"

int main(void){
    Obj *a = new Obj();
    std::vector<Obj> vec;
    vec.push_back(*a);

    Obj b = vec[0];

    vec.erase(vec.begin(),vec.end());
    delete a;
    _CrtDumpMemoryLeaks();
    return 0;
}
William McCarty
  • 769
  • 3
  • 12
  • 26

1 Answers1

6

Your vector hasn't gone out of scope yet when you call the leak checker.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • And @Drew Dormann said in a comment, std::vector::erase() does not promise to release memory. You don't really want it to, either because if you reuse the vector which is normal behavior after an erase(), it would have to allocate more memory to replace what it had just deallocated. – Dale Wilson Jun 27 '14 at 21:15