Thread: traping leaking memory

  1. #1
    Registered User
    Join Date
    Sep 2001

    traping leaking memory

    hi again. again on the subject of dynamic memory allocation for arrays of strings, or whatever, is there any c function or any other way in general that catches memory leaks or tell how much mem is used at any point (i asume that if i make a log file with the free mem at regular intervals it may be possible to identify memory leaks)?

    in general, how can i see the mem my prog uses at any time?

    moreover, is there any trick to imitate destructors in c?

    i'm asking this after realising that i was forgeting to free the mem i got from malloc...



  2. #2
    When you dynamically allocate memory, you always have to remember to deallocate it. That's one of the problems with C, if you forget then you have a memory leak and there's no way to check for it.

  3. #3
    Registered User cody's Avatar
    Join Date
    Sep 2001

    Little tool


    well there is one little thing you could do... Write a little program that counts the number of 'malloc' and 'new' statements in your sourcefiles and of course the number of 'free' and 'delete' statements.

    Of course there can still be memory leaks and the programm might be confused in loops ( advanced version you also check loops, but it's realy tricky I think ), but might be worth a try It's a nice practice task for newcomers, too. And it's something you can later really use ... maybe

    #include "reallife.h"

  4. #4
    Join Date
    Aug 2001
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    When you dynamically allocate memory you should try to free that memory in the same function that you defined an instance of the object. This does not always have to be done however there are several situations where you can otherwise run into problems, I'm not going to recite them because I'd have to research that, but I have come across these situations and read about them. After you free memory set the pointer to NULL because if it is accidently deleted twice than it is safe to do so while it is pointing to NULL but not otherwise. It is also safe I believe to dereference a NULL pointer.
    I compile code with:
    Visual Studio.NET beta2

  5. #5
    two things I usually do when I programme in C to detect/avoid memleak.
    1) Use TOP utility (if you are using Unix), if the program leaks memory, the 'size' column will keep on increasing as you execute the mem leak functions.

    2) Modularise code into functions and reuse the functions whenever possible. All you need to do is to ensure that the function is not leaky.

    Good luck

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > i'm asking this after realising that i was forgeting to free the mem i got from malloc...
    A sort of tip.

    As soon as you've finished writing your malloc code, break off and write the matching free code.

    I used to do the same thing with writing {}, "" and () pairs, until I got an editor which does some of them for me.

    There are $$$ tools like boundschecker and purify which help with this task (finding where its all gone wrong that is).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM