Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Visual Studio 2005 complains about memory leaks

1 view
Skip to first unread message

Rune Allnor

unread,
Jul 22, 2008, 5:31:03 PM7/22/08
to
Hi all.

Although this post concerns a particular compiler I hope the moderators
accept the question as it touches on my general understanding of RAII.
Or maybe lack of such.

I have a program where I allocate a number of data items in containers
of type std::set and std::string.

When I run the program in debug mode (VS2005) the execution monitor
(or whatever the correct term is under VS) complains about 'memory leaks'.

In the documentation at

http://msdn.microsoft.com/en-us/library/aa293906.aspx

I find the statement "A memory leak occurs when you allocate memory on
the heap and never deallocate that memory..." It is true that *I* don't deallocate
that memory, because I expect the destructor of the various containers to
do so when they go out of scope, according to RAII.

So my question is if I am right in that RAII ought to handle things without
my explicitly mentioning 'delete' in the code (and thus that the compiler
complains unecessarily), or if such issues are handled correctly by the
compiler and this error message points to some fundamental flaw in my
understanding of RAII. Or worse, in my program.

As far as I can tell from the data dump, the problem occurs in a std::set
which contains std::strings,

std::set<std::string> data;

but I haven't been able to pin-point the offending variables with any certainty.

Rune

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

Rune Allnor

unread,
Jul 22, 2008, 11:04:57 PM7/22/08
to
On 22 Jul, 23:31, Rune Allnor <all...@tele.ntnu.no> wrote:

> When I run the program in debug mode (VS2005) the execution monitor
> (or whatever the correct term is under VS) complains about 'memory leaks'.

...


> As far as I can tell from the data dump, the problem occurs in a std::set
> which contains std::strings,
>
> std::set<std::string> data;

Immediate problem solved: I use a factory pattern to set up a rather
large data structure. The data dump contained items which made me
suspect that data structure. I didn't understand what was going
on because I handled the data structure correctly in the main
program.

The problem turned out to be a bug in the factory, not the main
program.

> but I haven't been able to pin-point the offending variables with any certainty.

This probably veers off topic wrt this newsgroup, but if anybody
has a hint on how to identify the offending data items when
working with VS, I would be very interested in hearing about it.

marlow...@googlemail.com

unread,
Jul 24, 2008, 7:58:20 AM7/24/08
to
> This probably veers off topic wrt this newsgroup, but if anybody
> has a hint on how to identify the offending data items when
> working with VS, I would be very interested in hearing about it.
>
> Rune

It depends if the code is particular to the Visual Studio environment
(i.e it only works there and nowhere else). If the code is platform-
independent you could try using valgrind. Otherwise you may need to
use purify, which is available for Windoze (but it is rather
expensive).

Regards,

Andrew Marlow

Martin T.

unread,
Jul 28, 2008, 4:58:23 AM7/28/08
to
{ Accepted as a follow-up, though clearly environment-specific.
Further discussions on this matter are better directed to other
environment-specific groups. -mod }

Rune Allnor wrote:
> On 22 Jul, 23:31, Rune Allnor <all...@tele.ntnu.no> wrote:
>
>> When I run the program in debug mode (VS2005) the execution monitor
>> (or whatever the correct term is under VS) complains about 'memory leaks'.
> ...

>> but I haven't been able to pin-point the offending variables with any certainty.
>
> This probably veers off topic wrt this newsgroup, but if anybody
> has a hint on how to identify the offending data items when
> working with VS, I would be very interested in hearing about it.
>

The memory leak detection mechanism is part of MS CRT and I think every
C++ developer working with Visual Studio (of which I suspect there are
quite a few) should have some understanding of how this works. (*If* you
add MFC to the picture it get's even more complicated, but I'll spare
you that.)

Without too much typing I can offer a few links and search-terms, that
should get you started:
* http://winter.eecs.umich.edu/soarwiki/Tracking_down_memory_leaks
( Note: To manipulate the _crtBreakAlloc Variable in the VS debugger you
may have to find its address with {,,msvcr80d.dll} __p__crtBreakAlloc()
and edit the memory directly in a memory window)

* http://support.microsoft.com/kb/q151585/

You should probably also read the docs for <crtdbg.h> and the debug
functions, esp. _CrtSetDbgFlag().

br,
Martin

0 new messages