Visual Studio 2005 complains about memory leaks - c++

This is a discussion on Visual Studio 2005 complains about memory leaks - c++ ; 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 ...

+ Reply to Thread
Results 1 to 5 of 5

Visual Studio 2005 complains about memory leaks

  1. Default Visual Studio 2005 complains about memory leaks

    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! ]


  2. Default Re: Visual Studio 2005 complains about memory leaks

    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.

    Rune

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


  3. Default Re: Visual Studio 2005 complains about memory leaks

    > 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

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


  4. Default Re: Visual Studio 2005 complains about memory leaks

    { 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/soarwik...n_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

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


  5. Default Re: Visual Studio 2005 complains about memory leaks

    The memory leak information is printed on "Output" window.
    [ This advantage is in "Debug" mode only. ]
    Example

    int _tmain()
    {
    int* ptr = NULL;

    ptr = (int*)malloc(sizeof(int)*20);

    for( int i=0; i<20; ++i )
    ptr[i] = i;

    #ifndef NDEBUG

    int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); // Get current flag

    flag |= _CRTDBG_LEAK_CHECK_DF; // Turn on leak-checking bit

    _CrtSetDbgFlag(flag); // Set flag to the new value

    #endif

    //free(ptr);

    printf("\n\nPress Enter...");
    getch();
    return 0;
    }
    Cheers,
    Eliza

+ Reply to Thread