Local Heap?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William L. Bahn

    #1

    Local Heap?

    I am having a strange problem - and I suspect it is something
    specific to my compiler - but want to get a read on the Standard
    C portion of it first.

    Basic Question: Under the C Standard, under what conditions, if
    any, can a value returned by malloc() not be used in a subsequent
    free() call?

    I have a program that does a lot of things. I don't have the code
    handy to post, but I think a word description will do. I'd be
    more than happy to post code if someone thinks it would be
    helpful.

    One function "dynamicall y copies a string" - so I pass a pointer
    to a string, it checks the length of the string, allocates
    sufficient memory, copies the string into the new memory, and
    returns the value of the new pointer.

    I have another function that does a few things, one of which is
    to call the dynamic string copy function. It then does some other
    things and returns the pointer to the new string to main(). That
    works just fine (yes, including the null terminator). In main(),
    I perform some manipulations on that string and then free the
    pointer. That works fine.

    I then made a copy of the function - literally - and changed the
    name slightly and changed some of the miscellaneous details that
    it does - but the dynamic copy works the same. When I run it, I
    get a run time error when I call free() in main() and looking
    through the debug headers it appears that this new version is
    trying to free up memory that is not in the "Local Heap Space".
    I've never heard of that? What gives?

    I checked the value of the pointer within the function and the
    value that gets returned and the value in the pointer at the time
    that free() is called and they all agree. I copied the code from
    the original function into the new function so that the only
    remaining difference is the function names. The old function
    works, the new function throws the error. The only way I can get
    my program to run is to not perform the garbage collection?

    Any thoughts?





  • Karthik Kumar

    #2
    Re: Local Heap?

    William L. Bahn wrote:
    [color=blue]
    > I am having a strange problem - and I suspect it is something
    > specific to my compiler - but want to get a read on the Standard
    > C portion of it first.
    >
    > Basic Question: Under the C Standard, under what conditions, if
    > any, can a value returned by malloc() not be used in a subsequent
    > free() call?
    >
    > I have a program that does a lot of things. I don't have the code
    > handy to post, but I think a word description will do. I'd be
    > more than happy to post code if someone thinks it would be
    > helpful.[/color]

    Anyday, few lines of C code would be better compared to plain
    English !!
    [color=blue]
    >
    > One function "dynamicall y copies a string" - so I pass a pointer
    > to a string, it checks the length of the string, allocates
    > sufficient memory, copies the string into the new memory, and
    > returns the value of the new pointer.
    >
    > I have another function that does a few things, one of which is
    > to call the dynamic string copy function. It then does some other
    > things and returns the pointer to the new string to main(). That
    > works just fine (yes, including the null terminator). In main(),
    > I perform some manipulations on that string and then free the
    > pointer. That works fine.
    >
    > I then made a copy of the function - literally - and changed the
    > name slightly and changed some of the miscellaneous details that
    > it does - but the dynamic copy works the same. When I run it, I
    > get a run time error when I call free() in main() and looking
    > through the debug headers it appears that this new version is
    > trying to free up memory that is not in the "Local Heap Space".
    > I've never heard of that? What gives?[/color]

    What are those 'miscellaneous details' ?

    What is the name of the function that you had chosen ? Are you sure
    that there is no pollution of the function names here ?

    [color=blue]
    >
    > I checked the value of the pointer within the function and the
    > value that gets returned and the value in the pointer at the time
    > that free() is called and they all agree. I copied the code from
    > the original function into the new function so that the only
    > remaining difference is the function names. The old function
    > works, the new function throws the error. The only way I can get
    > my program to run is to not perform the garbage collection?
    >
    > Any thoughts?[/color]

    Please post the source code ( a small, compilable one that describes
    the problem precisely) for people to help.

    --
    Karthik.

    Comment

    • kevin.bagust

      #3
      Re: Local Heap?

      In article <10jdorodjjb6ba d@corp.supernew s.com>,
      William L. Bahn <william@toomuc hspam.net> wrote:[color=blue]
      > I am having a strange problem - and I suspect it is something
      > specific to my compiler - but want to get a read on the Standard
      > C portion of it first.[/color]

      Strange problem, it sounds like undefined behavoiur somewhere in your
      program, rather than a compiler problem.
      [color=blue]
      > Basic Question: Under the C Standard, under what conditions, if
      > any, can a value returned by malloc() not be used in a subsequent
      > free() call?[/color]

      Two possiblities:
      1) You have already freed that section of memory.
      2) You have invoked undefined behaviour somewhere in your program.

      <snip>
      [color=blue]
      > I then made a copy of the function - literally - and changed the
      > name slightly and changed some of the miscellaneous details that
      > it does - but the dynamic copy works the same. When I run it, I
      > get a run time error when I call free() in main() and looking
      > through the debug headers it appears that this new version is
      > trying to free up memory that is not in the "Local Heap Space".
      > I've never heard of that? What gives?[/color]

      It sounds like somewhere in your program you are writing outside of a
      valid object. Which is doing strange things to malloc memory area. This
      could be writing before the start or after the end of a malloced area of
      memory, writing through a uninitialized pointer.
      [color=blue]
      > Any thoughts?[/color]

      You could try Linting the code, or using a malloc debugging library if
      there is one avalible for your platform.

      These are only guesses, to get more help you need to post a version of
      code that has had as much code as possible removed from it but still shows
      the problem.

      Kevin.

      Comment

      • pete

        #4
        Re: Local Heap?

        William L. Bahn wrote:[color=blue]
        >
        > I am having a strange problem - and I suspect it is something
        > specific to my compiler - but want to get a read on the Standard
        > C portion of it first.
        >
        > Basic Question: Under the C Standard, under what conditions, if
        > any, can a value returned by malloc() not be used in a subsequent
        > free() call?[/color]

        If that value has already been used in a free() call,
        since the time when it was returned by malloc.

        --
        pete

        Comment

        • CBFalconer

          #5
          Re: Local Heap?

          "William L. Bahn" wrote:[color=blue]
          >
          > I am having a strange problem - and I suspect it is something
          > specific to my compiler - but want to get a read on the Standard
          > C portion of it first.
          >
          > Basic Question: Under the C Standard, under what conditions, if
          > any, can a value returned by malloc() not be used in a subsequent
          > free() call?[/color]

          If it already has been freed.

          --
          A: Because it fouls the order in which people normally read text.
          Q: Why is top-posting such a bad thing?
          A: Top-posting.
          Q: What is the most annoying thing on usenet and in e-mail?


          Comment

          • Lew Pitcher

            #6
            Re: Local Heap?

            -----BEGIN PGP SIGNED MESSAGE-----
            Hash: SHA1

            CBFalconer wrote:
            [color=blue]
            > "William L. Bahn" wrote:
            >[color=green]
            >>I am having a strange problem - and I suspect it is something
            >>specific to my compiler - but want to get a read on the Standard
            >>C portion of it first.
            >>
            >>Basic Question: Under the C Standard, under what conditions, if
            >>any, can a value returned by malloc() not be used in a subsequent
            >>free() call?[/color]
            >
            >
            > If it already has been freed.[/color]

            Couldn't you add
            "or, if it already has been passed to the realloc() function"
            to this statement?

            - --
            Lew Pitcher
            IT Consultant, Enterprise Application Architecture,
            Enterprise Technology Solutions, TD Bank Financial Group

            (Opinions expressed are my own, not my employers')
            -----BEGIN PGP SIGNATURE-----
            Version: GnuPG v1.2.4 (MingW32)

            iD8DBQFBNxCiagV FX4UWr64RAhgcAK CZphoUPjYE84VzT TiflqX5ZfiFEQCg 0e6g
            Fhbq90fm0AgHNiW SRtPTS6o=
            =1Bg4
            -----END PGP SIGNATURE-----

            Comment

            • Michael Kurz

              #7
              Re: Local Heap?

              "William L. Bahn" <william@toomuc hspam.net> wrote in message news:<10jdorodj jb6bad@corp.sup ernews.com>...[color=blue]
              >
              > Basic Question: Under the C Standard, under what conditions, if
              > any, can a value returned by malloc() not be used in a subsequent
              > free() call?
              >[/color]

              Normally this should not happen, but:
              1.
              -I once worked on a C Compiler for a Microcontroller , which had broken
              a
              Clib, as if it really ran out of memory (no physical memory left)
              malloc() did not return 0, although the memory was not allocated, but
              this is very rare. :-)

              2.
              You accidently freed the memory with free previously. (but as you
              already said
              you dont, well)

              3.
              Memory corruption
              Where is the address stored? Global data?, stack?
              If its stored on a stack variable it could possibly be a corrupted
              stack
              (Write an array variable over its real size, ...)

              A similar thing could happen if its a global.

              So what I would do is:
              Look if the address value is the same when it is allocated and when it
              is freed.
              If not well, then you know its probably a memory (global, stack)
              corruption :-).

              Regards
              Michael

              Comment

              • CBFalconer

                #8
                Re: Local Heap?

                Lew Pitcher wrote:[color=blue]
                > CBFalconer wrote:[color=green]
                >> "William L. Bahn" wrote:
                >>[color=darkred]
                >>> I am having a strange problem - and I suspect it is something
                >>> specific to my compiler - but want to get a read on the Standard
                >>> C portion of it first.
                >>>
                >>> Basic Question: Under the C Standard, under what conditions, if
                >>> any, can a value returned by malloc() not be used in a subsequent
                >>> free() call?[/color]
                >>
                >> If it already has been freed.[/color]
                >
                > Couldn't you add
                > "or, if it already has been passed to the realloc() function"
                > to this statement?[/color]

                Not necessarily. It may be so. However some system packages (my
                nmalloc especially) go to pains to avoid unnecessary copying and
                thus to return the same pointer if possible. Anytime realloc
                returns NULL it is possible to free the original pointer, assuming
                nothing else has fouled it.

                --
                A: Because it fouls the order in which people normally read text.
                Q: Why is top-posting such a bad thing?
                A: Top-posting.
                Q: What is the most annoying thing on usenet and in e-mail?


                Comment

                • Matthew Fisher

                  #9
                  Re: Local Heap?

                  "William L. Bahn" <william@toomuc hspam.net> wrote in message news:<10jdorodj jb6bad@corp.sup ernews.com>...[color=blue]
                  > I am having a strange problem - and I suspect it is something
                  > specific to my compiler - but want to get a read on the Standard
                  > C portion of it first.
                  >
                  > Basic Question: Under the C Standard, under what conditions, if
                  > any, can a value returned by malloc() not be used in a subsequent
                  > free() call?
                  >[/color]

                  William,

                  You have a clear case for heap corruption. (Mis)use of malloc/free
                  can lead to the heap being corrupted. One the heap is corrupted
                  results from malloc and free are both undefined. In you particular
                  case, you may a freed a global or stack variable. A subsquent malloc
                  would then return that value as a pointer. The final call to free
                  then is totally confused.

                  There are several freeware tools to help with this problems. Check
                  out Valgrind and mpatrol. My company, Dynamic Memory Solutions,
                  markets the commercial tool, Dynamic Leak Check (DLC). The DLC
                  catches heap corrupting error and leak by replacing the normal
                  malloc/free routine as runtime. See www.dynamic-memory.com

                  Good luck,
                  Matthew
                  Dynamic Memory Solutions

                  Comment

                  • CBFalconer

                    #10
                    Re: Local Heap?

                    Matthew Fisher wrote:[color=blue]
                    > "William L. Bahn" <william@toomuc hspam.net> wrote in message
                    >[color=green]
                    > > I am having a strange problem - and I suspect it is something
                    > > specific to my compiler - but want to get a read on the Standard
                    > > C portion of it first.
                    > >
                    > > Basic Question: Under the C Standard, under what conditions, if
                    > > any, can a value returned by malloc() not be used in a subsequent
                    > > free() call?[/color]
                    >
                    > You have a clear case for heap corruption. (Mis)use of malloc/free
                    > can lead to the heap being corrupted. One the heap is corrupted
                    > results from malloc and free are both undefined. In you particular
                    > case, you may a freed a global or stack variable. A subsquent malloc
                    > would then return that value as a pointer. The final call to free
                    > then is totally confused.
                    >
                    > There are several freeware tools to help with this problems. Check
                    > out Valgrind and mpatrol. My company, Dynamic Memory Solutions,
                    > markets the commercial tool, Dynamic Leak Check (DLC). The DLC
                    > catches heap corrupting error and leak by replacing the normal
                    > malloc/free routine as runtime. See www.dynamic-memory.com
                    >
                    > Good luck,
                    > Matthew
                    > Dynamic Memory Solutions
                    > www.dynamic-memory.com[/color]

                    This continuous vending of an apparently obviously flawed
                    commercial system is getting on my nerves. There is a free
                    implementation of malloc/free/realloc, which also affords fairly
                    comprehensive debugging tools, designed for the DJGPP system and
                    which I believe will function quite adequately on any Linux system
                    that supplies sbrk(). It is NOT standard C, and uses GNU flavor
                    (not C99) variadic macros for debugging itself (thus they can be
                    removed with care).

                    There is no guarantee that any replacement for malloc etc. will
                    function in a C system, the very act of so doing is expressly
                    forbidden by the standard. It is probably especially obnoxious in
                    a C++ system, but that is OT here (F'ups set).

                    See <http://cbfalconer.home .att.net/download/nmalloc.zip>

                    --
                    "A man who is right every time is not likely to do very much."
                    -- Francis Crick, co-discover of DNA
                    "There is nothing more amazing than stupidity in action."
                    -- Thomas Matthews


                    Comment

                    Working...