Stupid vc++ question, memory leaks

J

jschvat

I get the following output in vs2005:

'AllocationTest.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No symbols
loaded. Detected memory leaks!
Dumping objects ->
{263} normal block at 0x00356CD8, 564 bytes long.
Data: <H 5 5 5 5 > 48 A0 35 00 C8 A0 35 00 88 A0 35 00 08 A1 35 00
Object dump complete.
The program '[2212] AllocationTest.exe: Native' has exited with code 0 (0x0).

with this code... what am I missing?

#include "crtdbg.h"
#include <iostream>
#include <vector>

using namespace std;
class Allocator {

private:
int k;
public:
Allocator(int value) { k=value;}
~Allocator() { cout<<"deleting : "<<k<<endl; }
int value() { return k; }
void assign(int x) {k=x;}
};

int _tmain(int argc, _TCHAR* argv[])
{
int index;
int const count=100;
vector<Allocator *> v;
Allocator *t;

for(index=0;index<count;index++) {
t=new Allocator(index);
v.push_back(t);
}

for(index=0;index<count;index++) {
delete v[index];
}

_CrtDumpMemoryLeaks();
return 0;

}


thanks
 
C

Carl Daniel [VC++ MVP]

jschvat said:
I get the following output in vs2005:

'AllocationTest.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No
symbols loaded. Detected memory leaks!
Dumping objects ->
{263} normal block at 0x00356CD8, 564 bytes long.
Data: <H 5 5 5 5 > 48 A0 35 00 C8 A0 35 00 88 A0 35 00 08 A1 35
00 Object dump complete.
The program '[2212] AllocationTest.exe: Native' has exited with code
0 (0x0).

with this code... what am I missing?

You're not letting the vector itself go out of scope. Try putting a scope
around the vector declaration and all of the code that references it, but
not including the call to _CrtDumpMemoryLeaks.

(By "put a scope around" I mean enclose the code in an additional set of
braces, or move the test code to a function called from main(), while
keeping the call to _CrtDumpMemoryLeaks in main).

Even with that change, you might still see a "leak" if the standard library
caches memory (I don't recall if it does or not).

-cd
 
B

Bruno van Dooren

On top of Carl's answer, I'd like to point out that _crtdumpmemoryleaks
shows all memory that is still allocated at that point, so the term memory
leak is a bit deceptive.
The vector will likely have some heap memory still allocated, which is what
you are seeing.

--

Kind regards,
Bruno van Dooren MVP - VC++
http://msmvps.com/blogs/vanDooren
(e-mail address removed)
 
J

jschvat

On top of Carl's answer, I'd like to point out that _crtdumpmemoryleaks
shows all memory that is still allocated at that point, so the term memory
leak is a bit deceptive. The vector will likely have some heap memory
still allocated, which is what you are seeing.

Thanks for your help, that's what it was. I tried boundschecker it found
nothing. I was just curious about using vector for ptr storage. Although from
the articles I am seeing I think I should use a smart pointer and ptr_vector
instead. thank you - really appreciate the guidance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top