CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    Memory Leak Question

    Hi,
    Code:
    void A::A()
    {
        B *b = new B;
        b->Setup(....);
    }
    I don't understand why when b is created on the stack. It would cause memory leaks.
    I commented that part out, the leaks were gone. Any thoughts?
    Thanks
    Jack

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Memory Leak Question

    There's a simple rule to avoiding memory leaks:
    For every new, delete.
    For every new[], delete[].
    For every malloc(), free().

    Things aren't always that simple, of course, but here they are----you've allocated a B on the heap, stored its address only in a local pointer (so far as you've shown us), and not cleaned it up anywhere. Textbook memory leak.

  3. #3
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Memory Leak Question

    B is not created on the stack , it is on the heap cause *b is getting its address from new keyword , so there you have it , as Lindley pointed out just follow these simple rules you'be all right.

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Memory Leak Question

    Quote Originally Posted by lucky6969b View Post
    Hi,
    Code:
    void A::A()
    {
        B *b = new B;
        b->Setup(....);
    }
    I don't understand why when b is created on the stack. It would cause memory leaks.
    I commented that part out, the leaks were gone. Any thoughts?
    Thanks
    Jack
    Ahem.

    b is a "pointer" it is created on the stack, and is destroyed, like any other stack variable.

    b does not "own" its allocated memory.

    It is not b that is leaking, but the "new B".

    Don't confuse pointer and pointee
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    Mar 2011
    Posts
    46

    Re: Memory Leak Question

    Exactly as monarch and lindley says ... It is no different to normal c or c++ code

    void funcA(void){
    void* b;

    b = malloc(256);

    // do nothing or do whatever you want with b
    // if you dont do free(b);
    // you will bleed in my case 256 bytes of memory

    }

    New and malloc do the same sort of thing they allocate memory, free and delete release allocated memory

  6. #6
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Memory Leak Question

    The answer is to not use bare pointers unless you have to or they are just observers. Any time ownership is concerned you should use a smart pointer such as boosts shared_ptr or std::shared_ptr if your compiler supports it. Then when the smart pointer goes out of scope the memory will be automatically reclaimed by the smart pointers destructor ( or at least its reference count will be decremented and when that hits zero the object will be deleted ). Read up on the principle of 'Resource Acquisition Is Initialisation (RAII)' and smart pointers.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: Memory Leak Question

    ---
    Last edited by nuzzle; March 22nd, 2011 at 12:14 PM.

  8. #8
    Join Date
    May 2009
    Posts
    2,413

    Re: Memory Leak Question

    Quote Originally Posted by nuzzle View Post
    Others have already said it but this is another take on it,
    If you instead of this

    B *b = new B;

    think of it like this (which is logically equivalent),

    B *b;
    b = new B;

    the situation becomes clearer. The first line declares a pointer variable b of type B to be allocated on the stack. The second line allocates a B type object on the heap and assigns its pointer to b. It's only the allocation of the B object on the heap that can cause a leak. To prevent it you must put in this line when the object isn't used anymore.

    delete b;

    Some languages with built-in automatic garbage collection, such as Java and C#, will handle the deletion automatically but not C++. In C++ every new must have a matching delete (and every new[] a delete[]) or you have a memory leak on your hands.

    When ever it becomes hard to determine when and where an object is to be deleted it's time to consider putting a so called reference counting "smart" pointer in charge of the situation. It will delete the object when it's no longer pointed to from anywhere anymore and thus can be safely deleted.
    Last edited by nuzzle; March 22nd, 2011 at 01:37 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured