memory leak?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • questions?

    #1

    memory leak?

    I am new to C programming.
    I just wrote a quite long program for my project, but I have memory
    leak and the program keeps asking for more and more memory.

    What are the basic rules for checking potential places for memory leak?
    Are they any program to check which line is asking for a lot of
    memeory?

    Thanks a lot

  • Mark McIntyre

    #2
    Re: memory leak?

    On 29 Mar 2006 11:31:34 -0800, in comp.lang.c , "questions? "
    <universal_used @hotmail.com> wrote:
    [color=blue]
    >What are the basic rules for checking potential places for memory leak?[/color]

    Everything that is *alloc'ed must be free'd. There's not much more to
    it than that.
    [color=blue]
    >Are they any program to check which line is asking for a lot of
    >memeory?[/color]

    Sure, several. A websearch may turn them up.
    Mark McIntyre
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan

    Comment

    • David Resnick

      #3
      Re: memory leak?

      questions? wrote:[color=blue]
      > I am new to C programming.
      > I just wrote a quite long program for my project, but I have memory
      > leak and the program keeps asking for more and more memory.
      >
      > What are the basic rules for checking potential places for memory leak?
      > Are they any program to check which line is asking for a lot of
      > memeory?
      >
      > Thanks a lot[/color]

      There are tools such as valgrind (open source) and purify ($$$) that
      may or may not be available for your platform. I like both.
      If you are interested in tools, asking in a group dedicated to
      your platform of interest is the right thing to do.

      A portable alternative is to have wrappers around malloc/calloc/realloc
      and free and log allocations/frees and the line of code that is doing
      them. Those logs plus some post-processing can help sort things out.
      This only works if the allocations being leaked go through these
      wrappers (and are not, say, in some libraries not under your control),
      but it can be a decent approach.

      -David

      Comment

      • August Karlstrom

        #4
        Re: memory leak?

        questions? wrote:[color=blue]
        > I am new to C programming.
        > I just wrote a quite long program for my project, but I have memory
        > leak and the program keeps asking for more and more memory.
        >
        > What are the basic rules for checking potential places for memory leak?
        > Are they any program to check which line is asking for a lot of
        > memeory?[/color]

        If your memory allocation strategy is complicated and you are willing to
        sacrifice some speed (but gain reduced complexity) you can use a
        garbage collector, e.g. Hans Boehm's:




        August

        --
        I am the "ILOVEGNU" signature virus. Just copy me to your
        signature. This email was infected under the terms of the GNU
        General Public License.

        Comment

        • Ben C

          #5
          Re: memory leak?

          On 2006-03-30, August Karlstrom <fusionfive@com hem.se> wrote:[color=blue]
          > questions? wrote:[color=green]
          >> I am new to C programming.
          >> I just wrote a quite long program for my project, but I have memory
          >> leak and the program keeps asking for more and more memory.
          >>
          >> What are the basic rules for checking potential places for memory leak?
          >> Are they any program to check which line is asking for a lot of
          >> memeory?[/color][/color]

          You can use valgrind with its memcheck tool on some systems.

          Official Home Page for valgrind, a suite of tools for debugging and profiling. Automatically detect memory management and threading bugs, and perform detailed profiling. The current stable version is valgrind-3.27.1.


          There are other tools like this.

          Or just write little wrappers for malloc and free that keep track of
          everything. __FILE__ and __LINE__ can be useful for this-- your memory
          tracker can keep track of where in the source the calls came from.

          Something like this:

          void *my_malloc(size _t size, const char *file, unsigned line)
          {
          void *ret = malloc(size);
          static unsigned count = 0;

          /* store ret in a list somewhere perhaps */

          printf("%uth allocation of %p from %s:%u\n", count, ret, file, line);
          count++;
          return ret;
          }

          void my_free(void *p)
          {
          /* remove p from the list */

          printf("Freeing %p\n", p);
          free(p);
          }

          Then in something that's included by all source files:

          extern void *my_malloc(size _t, const char *, unsigned);
          extern void my_free(void *);

          #define malloc(size) my_malloc(size, __FILE__, __LINE__)
          #define free my_free

          At the end of the program, anything left in the list is leaked.
          Alternatively don't bother with a list, and just parse the output with a
          scripting language-- you've got a record of all the allocations and
          deallocations there. Do whichever's easier.

          You can tell from what got printed out which allocations leaked. If you
          run the program again (provided it doesn't depend on any external input
          that might change), you can use a debugger to investigate the leaked
          allocations as they are made.

          Another way to do this is to use a debugger like gdb as a tracer; you
          can set commands on a breakpoint to print a backtrace and then continue.
          This might help you find the leaking backtraces. But you might not need
          to resort to this.

          Comment

          • August Karlstrom

            #6
            Re: memory leak?

            Ben C wrote:[color=blue]
            > On 2006-03-30, August Karlstrom <fusionfive@com hem.se> wrote:
            >[color=green]
            >>questions? wrote:
            >>[color=darkred]
            >>>I am new to C programming.
            >>>I just wrote a quite long program for my project, but I have memory
            >>>leak and the program keeps asking for more and more memory.
            >>>
            >>>What are the basic rules for checking potential places for memory leak?
            >>>Are they any program to check which line is asking for a lot of
            >>>memeory?[/color][/color]
            >
            >
            > You can use valgrind with its memcheck tool on some systems.
            >
            > http://valgrind.org/[/color]

            [...]

            Please learn how to post/quote. Did you respond to what I wrote? No!
            Then don't reply to my message.


            Thanks,

            August

            --
            I am the "ILOVEGNU" signature virus. Just copy me to your
            signature. This email was infected under the terms of the GNU
            General Public License.

            Comment

            Working...