new and delete, please help

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

    new and delete, please help

    I create a new char[] and when I attempt to delete it, the debug gives this error (during
    runtime):

    Debug Error!

    Program: xxx

    DAMAGE: after Normal block (#58) at 0x00C315A0.


    I get this error everywhere I use delete []. When I remove all delete []'s from my code I
    don't get the errors (nor does the program sometimes crash in release version
    compilation), but I can't do that can I (memory leaks).

    This is a part of my code:

    ** BEGIN **

    int a = 0;
    char *name = NULL;
    a = GetWindowTextLe ngth(GetDlgItem (::hwnd, IDC_NAME)) + 1;
    if (!(name = new char[a]))
    {
    MessageBox(::hw nd,"Not enough memory to allocate the
    value!",NULL,MB _OK|MB_ICONERRO R);
    return;
    }
    GetDlgItemText( ::hwnd, IDC_NAME, name, a);
    // THIS GIVES THE ERROR:
    delete [] name;

    ** END **

    When I check the name value, it does give the expected value!

    I use MSVC++ 6.0 by the way.

    I am pretty new at programming C++ and any help is very much appreciated, I really can't
    figure out this one...

    --

    Regards,
    Spikinsson


  • Ron Natalie

    #2
    Re: new and delete, please help


    "Spikinsson " <not@gonna.tell .ya> wrote in message news:fRXOa.1300 8$7h.14117@afro dite.telenet-ops.be...

    [color=blue]
    > if (!(name = new char[a]))[/color]

    This can only be zero on broken compilers like VC++ 6.
    [color=blue]
    > GetDlgItemText( ::hwnd, IDC_NAME, name, a);[/color]

    Hopefully, this writes no more than "a" chars to name.

    The usual reasons delete blows up is because:

    1. Overflow the allocation.
    2. Delete something not alloc'd by new
    3. Delete something twice.

    It's not necessarily the thing that you are deleting that is the problem.
    Sometimes it's some other allocation that was messed up.


    Comment

    • Spikinsson

      #3
      Re: new and delete, please help

      "Ron Natalie" <ron@sensor.com > wrote[color=blue]
      > "Spikinsson " <not@gonna.tell .ya> wrote
      >[color=green]
      > > if (!(name = new char[a]))[/color]
      >
      > This can only be zero on broken compilers like VC++ 6.[/color]
      Thanks for your quick response, I don't quite understand what you're saying here
      actually...
      [color=blue][color=green]
      > > GetDlgItemText( ::hwnd, IDC_NAME, name, a);[/color]
      >
      > Hopefully, this writes no more than "a" chars to name.[/color]
      The definition GetDlgItemText says that the maximum characters written is a.
      [color=blue]
      >
      > The usual reasons delete blows up is because:
      >
      > 1. Overflow the allocation.[/color]
      I don't know what this is...
      [color=blue]
      > 2. Delete something not alloc'd by new[/color]
      As the code shows it is alloc'd by new.
      [color=blue]
      > 3. Delete something twice.[/color]
      Quite certain that this is not the case

      [color=blue]
      > It's not necessarily the thing that you are deleting that is the problem.
      > Sometimes it's some other allocation that was messed up.[/color]
      If I surround the delete [] with 2 message boxes, I get the first message box, then the
      error, than the second message box, so I figure that should be the erro...


      Comment

      • Ron Natalie

        #4
        Re: new and delete, please help


        "Spikinsson " <not@gonna.tell .ya> wrote in message news:a4YOa.1302 2$7h.14212@afro dite.telenet-ops.be...[color=blue]
        > "Ron Natalie" <ron@sensor.com > wrote[color=green]
        > > "Spikinsson " <not@gonna.tell .ya> wrote
        > >[color=darkred]
        > > > if (!(name = new char[a]))[/color]
        > >
        > > This can only be zero on broken compilers like VC++ 6.[/color]
        > Thanks for your quick response, I don't quite understand what you're saying here
        > actually...[/color]

        Standard C++ will either return the successful allocation or throw a bad_alloc exception.
        It will NEVER return null in the case above.
        [color=blue][color=green]
        > > 1. Overflow the allocation.[/color]
        > I don't know what this is...[/color]

        Write outside the characters returned (usually off the end, but off the beginning is sure to cause a crash).
        [color=blue][color=green]
        > > It's not necessarily the thing that you are deleting that is the problem.
        > > Sometimes it's some other allocation that was messed up.[/color]
        > If I surround the delete [] with 2 message boxes, I get the first message box, then the
        > error, than the second message box, so I figure that should be the erro...
        >[/color]
        Not necessarily. Consider the following:

        char* a = new char[10];
        char* b = new char[10];
        memset(b, 0, 100); // overlow the allocation of b.

        MESSAGE_BOX
        delete [] a; // BOOM!
        MESSAGE_BOX

        It's quite possible that some errant memory write or other abuse of new/delete has occurred.
        It's just the first use of new/delete AFTER that error has occured that is hanging you.

        Why not provide a complete minimal program that demonstrates your problem. The other
        option is to look at what your compiler provides to help diagnose there. VC++ in debug
        mode does have some stuff (look up mallocdbg or dbgmalloc, I don't remember) in the docs.


        Comment

        • Spikinsson

          #5
          Re: new and delete, please help

          "Ron Natalie" <ron@sensor.com > schreef in bericht
          news:3f0c4fc6$0 $87862$9a6e19ea @news.newshosti ng.com...[color=blue]
          >
          > "Spikinsson " <not@gonna.tell .ya> wrote in message[/color]
          news:a4YOa.1302 2$7h.14212@afro dite.telenet-ops.be...[color=blue][color=green]
          > > "Ron Natalie" <ron@sensor.com > wrote[color=darkred]
          > > > "Spikinsson " <not@gonna.tell .ya> wrote
          > > >
          > > > > if (!(name = new char[a]))
          > > >
          > > > This can only be zero on broken compilers like VC++ 6.[/color]
          > > Thanks for your quick response, I don't quite understand what you're saying here
          > > actually...[/color]
          >
          > Standard C++ will either return the successful allocation or throw a bad_alloc[/color]
          exception.[color=blue]
          > It will NEVER return null in the case above.
          >[color=green][color=darkred]
          > > > 1. Overflow the allocation.[/color]
          > > I don't know what this is...[/color]
          >
          > Write outside the characters returned (usually off the end, but off the beginning is[/color]
          sure to cause a crash).[color=blue]
          >[color=green][color=darkred]
          > > > It's not necessarily the thing that you are deleting that is the problem.
          > > > Sometimes it's some other allocation that was messed up.[/color]
          > > If I surround the delete [] with 2 message boxes, I get the first message box, then[/color][/color]
          the[color=blue][color=green]
          > > error, than the second message box, so I figure that should be the erro...
          > >[/color]
          > Not necessarily. Consider the following:
          >
          > char* a = new char[10];
          > char* b = new char[10];
          > memset(b, 0, 100); // overlow the allocation of b.
          >
          > MESSAGE_BOX
          > delete [] a; // BOOM!
          > MESSAGE_BOX
          >
          > It's quite possible that some errant memory write or other abuse of new/delete has[/color]
          occurred.[color=blue]
          > It's just the first use of new/delete AFTER that error has occured that is hanging you.
          >
          > Why not provide a complete minimal program that demonstrates your problem. The other
          > option is to look at what your compiler provides to help diagnose there. VC++ in debug
          > mode does have some stuff (look up mallocdbg or dbgmalloc, I don't remember) in the[/color]
          docs.[color=blue]
          >[/color]

          Thanks very much for your help, but I don't have the smallest clue how to successfully
          debug, and the program is quite big at the moment so I can't really send that. Thanks
          anyway and I'll try to look up some debug info...


          Comment

          • Spikinsson

            #6
            Re: new and delete, please help

            > Write outside the characters returned (usually off the end, but off the beginning is
            sure to cause a crash).[color=blue]
            >[color=green][color=darkred]
            > > > It's not necessarily the thing that you are deleting that is the problem.
            > > > Sometimes it's some other allocation that was messed up.[/color]
            > > If I surround the delete [] with 2 message boxes, I get the first message box, then[/color][/color]
            the[color=blue][color=green]
            > > error, than the second message box, so I figure that should be the erro...
            > >[/color]
            > Not necessarily. Consider the following:
            >
            > char* a = new char[10];
            > char* b = new char[10];
            > memset(b, 0, 100); // overlow the allocation of b.
            >
            > MESSAGE_BOX
            > delete [] a; // BOOM!
            > MESSAGE_BOX
            >
            > It's quite possible that some errant memory write or other abuse of new/delete has[/color]
            occurred.[color=blue]
            > It's just the first use of new/delete AFTER that error has occured that is hanging you.
            >
            > Why not provide a complete minimal program that demonstrates your problem. The other
            > option is to look at what your compiler provides to help diagnose there. VC++ in debug
            > mode does have some stuff (look up mallocdbg or dbgmalloc, I don't remember) in the[/color]
            docs.

            Ok, but I don't quite know how to do this, I will look into it and repost, also, in debug
            I get errors, and not in the release version, why? Are these errors even important, are it
            those errors which only are showed in debug that cause the release version to sometimes
            crash?


            Comment

            • Karl Heinz Buchegger

              #7
              Re: new and delete, please help



              Spikinsson wrote:[color=blue]
              >
              >
              > When I check the name value, it does give the expected value!
              >[/color]

              Looks like a bug in either MFC or Windows.

              Have you tried to allocate one more character then GetWindowTextLe ngth
              tells you in order to account for a terminating '\0' character? I could
              imagine that the situation is analogous to strlen. You need to allocate
              one character more then strlen tells you, because strlen returns the number
              of charcaters in a string, *not counting the terminating '\0'*

              len = strlen( some_text );
              name = new char [ len + 1 ];
              strcpy( name, some_text );

              Same here:

              len = GetWindowTextLe ngth( ... );
              name = new char [ len + 1 ];
              GetDlgItemText( ... );


              By the way, have you checked what GetWindowTextLe ngth returns
              and compared that to your expectations for a known text?. Could
              be your first encounter with a debugger to figure out that one.

              --
              Karl Heinz Buchegger
              kbuchegg@gascad .at

              Comment

              • Michiel Salters

                #8
                Re: new and delete, please help

                "Spikinsson " <not@gonna.tell .ya> wrote in message news:<fRXOa.130 08$7h.14117@afr odite.telenet-ops.be>...[color=blue]
                > I create a new char[] and when I attempt to delete it, the debug gives
                > this error (during runtime):
                >
                > Debug Error!
                >
                > Program: xxx
                >
                > DAMAGE: after Normal block (#58) at 0x00C315A0.
                >
                >
                > I get this error everywhere I use delete []. When I remove all delete []'s
                > from my code I don't get the errors (nor does the program sometimes
                > crash in release version compilation), but I can't do that can I
                > (memory leaks).[/color]

                Let me explain what happens here.
                When you do the new[] in debug mode, the compiler tacks on a few extra
                bytes without telling you. In these bytes a special pattern is written.
                You should not write more bytes than you asked for, so the pattern
                should remain untouched.

                Once you call delete[], the runtime checks if the pattern is still
                in place. If it's damaged, you wrote too many bytes. This gives the
                error above.

                Remove the delete[]s and the runtime won't do the check. This is of
                course a memory leak, and you still haven't solved the overwrite bug.

                What you need to do is checking if you have allocated enough bytes.
                Since you're dealing with char[]s, it might very well be a case of
                not counting a \0. There are lots of other possible bugs, so check
                all writes to the array.

                The real solution would be to use a string or container of chars,
                though. new[] char is a primitive.

                Regards,
                --
                Michiel Salters

                Comment

                • Spikinsson

                  #9
                  Re: new and delete, please help

                  "Karl Heinz Buchegger" <kbuchegg@gasca d.at> schreef in bericht
                  news:3F0D214B.5 24F24D3@gascad. at...[color=blue]
                  > Have you tried to allocate one more character then GetWindowTextLe ngth
                  > tells you in order to account for a terminating '\0' character? I could
                  > imagine that the situation is analogous to strlen. You need to allocate
                  > one character more then strlen tells you, because strlen returns the number
                  > of charcaters in a string, *not counting the terminating '\0'*[/color]

                  Thank you so much, that seems to have solved the problem! I'm really greatful for this,
                  now I created a function myself so I can edit it if I encounter more such problems.

                  char* new_array_point er(int size)
                  {
                  return (char *)malloc(size+1 );
                  }

                  void delete_array_po inter(char* a)
                  {
                  if (a != NULL)
                  free(a);
                  }

                  I don't use new now, but I think this is practically the same...
                  When I added the +1 to size everything was fixed! Thanks man!


                  Comment

                  • John Harrison

                    #10
                    Re: new and delete, please help


                    "Spikinsson " <not@gonna.tell .ya> wrote in message
                    news:bwfPa.1423 5$7h.14336@afro dite.telenet-ops.be...[color=blue]
                    > "Karl Heinz Buchegger" <kbuchegg@gasca d.at> schreef in bericht
                    > news:3F0D214B.5 24F24D3@gascad. at...[color=green]
                    > > Have you tried to allocate one more character then GetWindowTextLe ngth
                    > > tells you in order to account for a terminating '\0' character? I could
                    > > imagine that the situation is analogous to strlen. You need to allocate
                    > > one character more then strlen tells you, because strlen returns the[/color][/color]
                    number[color=blue][color=green]
                    > > of charcaters in a string, *not counting the terminating '\0'*[/color]
                    >
                    > Thank you so much, that seems to have solved the problem! I'm really[/color]
                    greatful for this,[color=blue]
                    > now I created a function myself so I can edit it if I encounter more such[/color]
                    problems.[color=blue]
                    >
                    > char* new_array_point er(int size)
                    > {
                    > return (char *)malloc(size+1 );
                    > }
                    >
                    > void delete_array_po inter(char* a)
                    > {
                    > if (a != NULL)
                    > free(a);
                    > }
                    >
                    > I don't use new now, but I think this is practically the same...
                    > When I added the +1 to size everything was fixed! Thanks man!
                    >[/color]

                    Maybe I'm missing something but you did post this code

                    a = GetWindowTextLe ngth(GetDlgItem (::hwnd, IDC_NAME)) + 1;
                    if (!(name = new char[a]))

                    According to that code you were adding one to GetWindowTextLe ngth.

                    john


                    Comment

                    • Spikinsson

                      #11
                      Re: new and delete, please help

                      "John Harrison" <john_andronicu s@hotmail.com> schreef in bericht
                      news:bek68e$68j lj$1@ID-196037.news.uni-berlin.de...[color=blue]
                      >
                      > "Spikinsson " <not@gonna.tell .ya> wrote in message
                      > news:bwfPa.1423 5$7h.14336@afro dite.telenet-ops.be...[color=green]
                      > > "Karl Heinz Buchegger" <kbuchegg@gasca d.at> schreef in bericht
                      > > news:3F0D214B.5 24F24D3@gascad. at...[color=darkred]
                      > > > Have you tried to allocate one more character then GetWindowTextLe ngth
                      > > > tells you in order to account for a terminating '\0' character? I could
                      > > > imagine that the situation is analogous to strlen. You need to allocate
                      > > > one character more then strlen tells you, because strlen returns the[/color][/color]
                      > number[color=green][color=darkred]
                      > > > of charcaters in a string, *not counting the terminating '\0'*[/color]
                      > >
                      > > Thank you so much, that seems to have solved the problem![/color][/color]
                      [color=blue]
                      >
                      > Maybe I'm missing something but you did post this code
                      >
                      > a = GetWindowTextLe ngth(GetDlgItem (::hwnd, IDC_NAME)) + 1;
                      > if (!(name = new char[a]))
                      >
                      > According to that code you were adding one to GetWindowTextLe ngth.
                      >
                      > john
                      >[/color]

                      Yes, but also one was added to the maximum, as I use a there also:

                      GetDlgItemText( ::hwnd, IDC_NAME, name, a);


                      Comment

                      • Karl Heinz Buchegger

                        #12
                        Re: new and delete, please help



                        John Harrison wrote:[color=blue]
                        >
                        >
                        > Maybe I'm missing something but you did post this code
                        >
                        > a = GetWindowTextLe ngth(GetDlgItem (::hwnd, IDC_NAME)) + 1;
                        > if (!(name = new char[a]))
                        >
                        > According to that code you were adding one to GetWindowTextLe ngth.
                        >[/color]

                        That's why I think it really is a bug in Windows (or MFC).
                        It seems that the GetText function takes the buffer size
                        + 1 extra character for the '\0'.

                        Haven't tested it though, just speculating.

                        --
                        Karl Heinz Buchegger
                        kbuchegg@gascad .at

                        Comment

                        Working...