C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Newbie memory leak question?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
jschvat
Guest





PostPosted: Sun May 13, 2007 2:44 am    Post subject: Newbie memory leak question? Reply with quote



{ Respondents are requested to focus on standard C++. -mod }


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 ahead, I appreciate the help.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Markus Schoder
Guest





PostPosted: Sun May 13, 2007 8:04 am    Post subject: Re: Newbie memory leak question? Reply with quote



On Sat, 12 May 2007 20:44:33 -0600, jschvat wrote:

Quote:
{ Respondents are requested to focus on standard C++. -mod }


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 ahead, I appreciate the help.

The vector v has very likely allocated heap memory to hold the pointers.
This memory will be freed when the vector is destroyed which is at the
end of the program and thus after you call the memory leak check function.

Try to put everything in the _tmain function before

_CrtDumpMemoryLeaks();

into a block.

--
Markus Schoder


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Guest






PostPosted: Sun May 13, 2007 6:36 pm    Post subject: Re: Newbie memory leak question? Reply with quote



"jschvat" <jschvat (AT) msn (DOT) com> wrote:
Quote:
{ Respondents are requested to focus on standard C++. -mod }

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;

}

There is no memory leak in that code. Your issue must be with your tool to
detect memory leaks.

Regards,

--
Ivan Novick
http://www.0x4849.net

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Al
Guest





PostPosted: Sun May 13, 2007 11:11 pm    Post subject: Re: Newbie memory leak question? Reply with quote

Hi,

jschvat wrote:
Quote:
{ Respondents are requested to focus on standard C++. -mod }


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?
snip
thanks ahead, I appreciate the help.

I've used these leak detection functions and sometimes their behavior is
not what you'd expect. As someone else mentioned, one possibility is
that things are being freed after main ends, therefore it "looks" like a
leak, but isn't.

One way to get around this is to install an exit handler at the
beginning of main, such as:

int main() {
atexit(_CrtDumpMemoryLeaks);
// ...;
}

But sometimes even that isn't enough, so what I believe MSDN recommends
is to do the following:

int main() {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF
| _CRTDBG_LEAK_CHECK_DF);
// ...;
}

This last version gets called later, therefore fewer false positives are
reported.

Hope that helps,
-Al-



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Martin Bonner
Guest





PostPosted: Mon May 14, 2007 7:41 pm    Post subject: Re: Newbie memory leak question? Reply with quote

On May 14, 12:11 am, Al <t...@haik.us> wrote:
Quote:
Hi,





jschvat wrote:
{ Respondents are requested to focus on standard C++. -mod }

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?
snip
thanks ahead, I appreciate the help.

I've used these leak detection functions and sometimes their behavior is
not what you'd expect. As someone else mentioned, one possibility is
that things are being freed after main ends, therefore it "looks" like a
leak, but isn't.

In general that is a problem. However in this case, the problem is
that the memory is freed in the destructor of the vector, and that is
not called until main returns. The solution is to put the vector in a
smaller scope - like so:

int main()
{
{
vector<Allocator *> v(100); // Create a vector of 100
empty elements, and then destroy it.
}
_CrtDumpMemoryLeaks(); // No leaks.
return 0;
}

Notes to the OP:

1. _tmain and TCHAR will get you slammed for being non-standard.
They are also pointless. You are never going to build both unicode
and ANSI versions of your software. Pick one and stick to it.
2. Allocator is a bad name for the class. It isn't an allocator, it
is a random class.
3. Both index and t are declared as if this were c. index should be
declared in both for loops. t should be a local variable in the inner
loop.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.