CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Memory Leak

  1. #1
    Join Date
    May 2004
    Posts
    81

    Memory Leak

    Hi

    What is meant by memory leak and how can we prevent it.

    Thanks in advance
    Sudhakar.M

  2. #2
    Join Date
    Jan 2001
    Posts
    588

    Re: Memory Leak

    In short, a memory leak is experienced when a program allocates a block of memory and never frees it to the operating system.

    A good way to prevent memory leaks is to avoid new/delete (or malloc/free) when writing C++ programs. Instead, use containers like std::vector, std::list, or std::map. Also, instead of using char * arrays to represent strings, use std::string.

  3. #3
    Join Date
    Jun 2003
    Posts
    258

    Re: Memory Leak

    Just an added note, it will be most obvious if you're allocating large blocks of data, or if you are allocating data in a loop and not freeing it. If you can't avoid new/delete, you can try using smart pointers. They have a couple of oddities, but they are very useful. Check it out here

    http://ootips.org/yonat/4dev/smart-pointers.html

    They have the added benefit of cleaning themselves up when they go out of scope. Good luck.

    ~Steve

  4. #4
    Join Date
    Nov 2004
    Posts
    92

    Re: Memory Leak

    Normally, you can throw exception to tell you that the memory is leaking!

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

  6. #6
    Join Date
    Nov 2004
    Location
    India
    Posts
    36

    Cool Re: Memory Leak

    The best way to know about a memory leak is overloading new and delete operators. In the new routine keep account of how much memory you have allocated and in delete routine keep account of memory that has been freed. So at the end you can flash a message showing the difference of total allocated and de-allocated memory. The difference is the MEMORY LEAK. It will show you how much bytes of memory is leaking. But it works with C++ only, not with C.
    All the Best.
    Mukesh Vijay

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Memory Leak

    See also the FAQ about memory leaks.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

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