std::vector mem leak question


1
2
3
4
5
6
7
8
9
10
11
12

void some_func()
{
   std::vectr<int> vec;
   int *ptr1 = new ptr(1);
   int *ptr2 = new ptr(2);

   vec.push_back(*ptr1);
   vec.push_pack(*ptr2);

   vec.clear();
}


My question is will this give me memory leaks for ptr1 and ptr2 that are allocated at the heap when this function exits.

Or I have to loop through the vector and manually delete the elements before clear().

Thanks
You're just adding a copy of *ptr1 and *ptr2 to the vector, so I imagine it's a memory leak.
Tested, just to be sure:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <vector>
// memory leak detection
#define _CRTDBG_MAP_ALLOC
#include <iostream>
#include <crtdbg.h>
#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif

void some_func()
{
   std::vector<int> vec;
   int *ptr1 = new int(1);
   int *ptr2 = new int(2);
   vec.push_back(*ptr1);
   vec.push_back(*ptr2);
   vec.clear();
}

int main() {
	some_func();
	_CrtDumpMemoryLeaks();
	return 0;
}

Detected memory leaks!
Dumping objects ->
c:\users\X\documents\visual studio 2010\projects\t\t\t.cpp(15) : {149} normal block at 0x00474F68, 4 bytes long.
 Data: <    > 02 00 00 00 
c:\users\X\documents\visual studio 2010\projects\t\t\t.cpp(14) : {148} normal block at 0x00474F28, 4 bytes long.
 Data: <    > 01 00 00 00 
Object dump complete.
The vector is not leaking.
ptr1 and ptr2 are leaking. The vector copies the values of the memory pointed to by ptr1 and ptr2, this can be done millions of times with no problems, but if you don't free the memory pointed to by 1 and 2 you have a leak, but it has absolutely nothing to do with the vector itself.
Use it or explicit delete each element.
1
2
3
4
std::vector<std::shared_ptr<int> > vec;
vec.push_back(std::shared_ptr<int>(new int(1)));
vec.push_back(std::shared_ptr<int>(new int(2)));
vec.clear();
thanks for the help guys.

so to avoid leaks in this situation is either use smart pointers to look after the objects or do something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

void some_func()
{
   std::vectr<int> vec;
   int *ptr1 = new ptr(1);
   int *ptr2 = new ptr(2);

   vec.push_back(*ptr1);
   vec.push_pack(*ptr2);

   delete ptr1;
   delete ptr2;

   vec.clear();
}

thanks in advance
Don't use new unless you have to.

There is no reason to use it here. You're better off just not using it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void some_func()
{
   std::vectr<int> vec;
   int v1 = 1;
   int v2 = 2;

   vec.push_back(v1);
   vec.push_pack(v2);

   vec.clear();
}

// or... more simply....

void some_func()
{
   std::vectr<int> vec;

   vec.push_back(1);
   vec.push_pack(2);

   vec.clear();
}
The only reasons I can think of to put pointers in a vector are polymorphism and for shared access to objects. Both of these cases are best handled by smart pointers.

Putting native pointers in a vector when they own dynamic memory is a disaster waiting to happen. If you forget to delete them for any reason (even something like an exception being thrown), you have a memory leak. If you access the data after deleting, you have a segmentation fault.
Topic archived. No new replies allowed.