memory leak problem.

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

    #1

    memory leak problem.

    Hello all..

    I am experiencing memory leakes in on of my applications that i am
    developing. the % of memory used for the application is increasing as
    shown in the top command on linux.

    The memory is leaking only when a function is called where i am
    allocating memory dynamically to a pointer(char string,i,e c-string).
    if i replace the logic with the use of stl string the memory leaking
    problem goes off..

    but i need to understand why the memory is leaking...

    the overall structure is as follows..

    void function()
    {

    char *str=new cahr[100];

    ///code where this string is manipulated and used....
    //delete str;
    }

    now this function is called numerous times while the application is
    running...

    i know that i need to deallocate the explicit mem allocation through
    free or delete...but after uncommenting the delete command the
    application is aborted as soon as this function is invoked when a
    specific condition is met durining application execution.
    the application is aborted with the following on the command prompt:
    *** glibc detected *** ./exe: munmap_chunk(): invalid pointer:
    0x000000001457f 85a ***
    then a backtrace log is generated...

    i am deleting the pointer only after its of no more use...then why is
    the application being aborted saying invalid pointer...
    and also after each of this function call...a newer activation frame
    for the function is generated so a new pointer should exist on each
    function call...

    How can the memory leaking be fixed in such a scenario?

    Kindly help.





  • red floyd

    #2
    Re: memory leak problem.

    ashjas wrote:
    Hello all..
    >
    I am experiencing memory leakes in on of my applications that i am
    developing. the % of memory used for the application is increasing as
    shown in the top command on linux.
    >
    The memory is leaking only when a function is called where i am
    allocating memory dynamically to a pointer(char string,i,e c-string).
    if i replace the logic with the use of stl string the memory leaking
    problem goes off..
    >
    but i need to understand why the memory is leaking...
    >
    the overall structure is as follows..
    >
    void function()
    {
    >
    char *str=new cahr[100];
    >
    ///code where this string is manipulated and used....
    //delete str;
    }
    >
    now this function is called numerous times while the application is
    running...
    >
    i know that i need to deallocate the explicit mem allocation through
    free or delete...but after uncommenting the delete command the
    application is aborted as soon as this function is invoked when a
    specific condition is met durining application execution.
    the application is aborted with the following on the command prompt:
    *** glibc detected *** ./exe: munmap_chunk(): invalid pointer:
    0x000000001457f 85a ***
    then a backtrace log is generated...
    >
    i am deleting the pointer only after its of no more use...then why is
    the application being aborted saying invalid pointer...
    and also after each of this function call...a newer activation frame
    for the function is generated so a new pointer should exist on each
    function call...
    >
    How can the memory leaking be fixed in such a scenario?
    >
    1. Memory allocated with new[] needs to be deallocated with delete[],
    not delete.

    2. Why don't you just use a std::vector? It will handle the memory
    management for you.

    Comment

    • Salt_Peter

      #3
      Re: memory leak problem.

      On Aug 26, 12:47 am, ashjas <ash...@gmail.c omwrote:
      Hello all..
      >
      I am experiencing memory leakes in on of my applications that i am
      developing. the % of memory used for the application is increasing as
      shown in the top command on linux.
      >
      The memory is leaking only when a function is called where i am
      allocating memory dynamically to a pointer(char string,i,e c-string).
      if i replace the logic with the use of stl string the memory leaking
      problem goes off..
      >
      but i need to understand why the memory is leaking...
      >
      the overall structure is as follows..
      >
      void function()
      {
      >
      char *str=new cahr[100];
      >
      ///code where this string is manipulated and used....
      //delete str;
      delete [] str;

      i see no reason to use the heap here, what's wrong with
      char str[100];
      why not use std::string ?
      or std::vector< char ?
      >
      }
      >
      now this function is called numerous times while the application is
      running...
      >
      i know that i need to deallocate the explicit mem allocation through
      free or delete...but after uncommenting the delete command the
      application is aborted as soon as this function is invoked when a
      specific condition is met durining application execution.
      the application is aborted with the following on the command prompt:
      *** glibc detected *** ./exe: munmap_chunk(): invalid pointer:
      0x000000001457f 85a ***
      then a backtrace log is generated...
      as expected
      >
      i am deleting the pointer only after its of no more use...then why is
      the application being aborted saying invalid pointer...
      because a pointer to a single char is not the same as a pointer to an
      array.
      you allocated an array and tried to delete a single char. Since str is
      not a pointer to a char you get the diagnostic.
      and also after each of this function call...a newer activation frame
      for the function is generated so a new pointer should exist on each
      function call...
      >
      How can the memory leaking be fixed in such a scenario?
      >
      Kindly help.

      Comment

      • Ismo Salonen

        #4
        Re: memory leak problem.

        red floyd wrote:
        ashjas wrote:
        >Hello all..
        >>
        >I am experiencing memory leakes in on of my applications that i am
        >developing. the % of memory used for the application is increasing as
        >shown in the top command on linux.
        >>
        >The memory is leaking only when a function is called where i am
        >allocating memory dynamically to a pointer(char string,i,e c-string).
        >if i replace the logic with the use of stl string the memory leaking
        >problem goes off..
        >>
        >but i need to understand why the memory is leaking...
        >>
        >the overall structure is as follows..
        >>
        >void function()
        >{
        >>
        >char *str=new cahr[100];
        >>
        >///code where this string is manipulated and used....
        >//delete str;
        >}
        >>
        >now this function is called numerous times while the application is
        >running...
        >>
        >i know that i need to deallocate the explicit mem allocation through
        >free or delete...but after uncommenting the delete command the
        >application is aborted as soon as this function is invoked when a
        >specific condition is met durining application execution.
        >the application is aborted with the following on the command prompt:
        >*** glibc detected *** ./exe: munmap_chunk(): invalid pointer:
        >0x000000001457 f85a ***
        >then a backtrace log is generated...
        >>
        >i am deleting the pointer only after its of no more use...then why is
        >the application being aborted saying invalid pointer...
        >and also after each of this function call...a newer activation frame
        >for the function is generated so a new pointer should exist on each
        >function call...
        >>
        >How can the memory leaking be fixed in such a scenario?
        >>
        >
        1. Memory allocated with new[] needs to be deallocated with delete[],
        not delete.
        >
        2. Why don't you just use a std::vector? It will handle the memory
        management for you.
        >
        Usually vector<charis bad choice, std::string is better. But or course
        it depends whether you are storing characters or binary data.
        But "char *str=new char[100];" is usually plain stupid,
        char str[100] has same effect but not the overhead of dynamic memory
        management.

        To OP : if program crashes at delete then you have should be looking
        memory corruption much earlier, buffer overruns etc.

        ismo

        Comment

        • Juha Nieminen

          #5
          Re: memory leak problem.

          ashjas wrote:
          How can the memory leaking be fixed in such a scenario?
          Use std::string. There's no need to use an explicitly allocated char
          array.

          Comment

          • Alain Ketterlin

            #6
            Re: memory leak problem.

            ashjas <ashjas@gmail.c omwrites:
            char *str=new cahr[100];
            >
            ///code where this string is manipulated and used....
            //delete str;
            Should be: delete [] str;

            -- Alain.

            Comment

            • Bernd Strieder

              #7
              Re: memory leak problem.

              Hello,

              ashjas wrote:
              I am experiencing memory leakes in on of my applications that i am
              developing. the % of memory used for the application is increasing as
              shown in the top command on linux.
              Since you are on linux, learn to use valgrind. It will tell you about
              lots of errors with new, delete and leaks, and it will tell you the
              location where errors happen. You should read about libstdc++ as well
              to turn off some of its allocation features to improve results of
              valgrind.

              Bernd Strieder

              Comment

              Working...