Thread: cleaning up memory leaks

  1. #1
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    cleaning up memory leaks

    is there any way to clean up a memory leak without restarting your computer using a C++ Program?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is there any way to clean up a memory leak without restarting your computer using a C++ Program?
    Unless your operating system reclaims lost memory after the program terminates, the only solution is a reboot. You're better off removing all memory leaks from your program before running it.
    My best code is written with the delete key.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >This scenario is pure speculation at best.
    Very true. I don't know of any common operating systems that do not do this. My point of course was to mention that there is no need to reboot.

    >Explicitly freeing memory before termination is completely a matter of style, and in some cases maintainability.
    This I don't agree with. I've seen many cases where a good application is reused as a library and run continuously as opposed to just once. In those situations a memory leak will show itself because the system can't reclaim memory until after the program has terminated. The program running this broken library will eventually run out of free memory. So yes, explicit memory release is a matter of style, but also a hedge against disaster if the application is reused for other purposes. Wouldn't you agree that it's better to be pedantic and write good code from the start instead of waiting until your bad code becomes a liability?
    My best code is written with the delete key.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    i know fixing a memory leak before running the program is best, but i was wondering because sometimes i find a memory leak after running the program and i don't feel like restarting teh computer... idk... laziness==bad;
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    an example would be not setting the end link on a linked list to NULL...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >an example would be not setting the end link on a linked list to NULL...
    That's not a memory leak, it's a dangling/uninitialized pointer. The typical result of not setting the end link on a linked list to null is an access violation when you try to dereference a pointer to memory that your program doesn't own. I'm assuming of course that you use something like this:
    Code:
    for (it = head; it != 0; it = it->next)
    {
        // Do something
    }
    This will cause trouble if the last valid node's next pointer is not null.
    My best code is written with the delete key.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    i'm sorry... i wasn't paying attention before... i meant when you run through the program without deleting the pointers... so the list is still in memory...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Post

    Code:
    template <class T>
    struct mmgr     {
    virtual mmgr(T * track = 0){ forget(), attach(track); }
    virtual ~mmgr() { free(); }
    T * attach(T * track){ free(); return memory = track; }
    T * forget() { T * oval = memory; memory = 0; return oval; }
    void free() { if(memory) delete[] memory;  memory = 0; }
    T * operator = (T * track){ return attach(track); }
    T * memory;    };
    You might use something like this to manage chunks of memory.

    mmgr <int> mgr = new int[1000];

    mgr = new int[500]; // o.k., 1000 ints freed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking for memory leaks
    By Bladactania in forum C Programming
    Replies: 5
    Last Post: 02-10-2009, 12:58 PM
  2. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. COM Memory Leaks
    By subdene in forum Windows Programming
    Replies: 0
    Last Post: 06-07-2004, 11:57 AM
  5. about memory leaks with this simple program
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 04-07-2002, 07:19 PM