Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. fault on delete[ ] ?

fault on delete[ ] ?

Scheduled Pinned Locked Moved C / C++ / MFC
csharpvisual-studiogame-devhelpquestion
11 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Brian C HartB Brian C Hart

    Hello all, I use VC6. Haven't the funds to bother with VS.NET yet... I have the weirdest problems happening lately, with this code: double* arr = new double[some_length]; if (arr == NULL) return; /* validity check */ //...use arr here ... delete[] arr; /* segmentation fault here -- wtf??? */ arr = NULL; I have no idea what could be causing this -- I mean, I am behaving myself, and calling delete for arrays of a primitive type that I allocate on the heap, right? The problem is there whether I call either delete[] or just plain delete. The problem goes away when I remove the call to delete[] entirely. same thing happens using malloc()/free()...what's going on? This is bizzare. Has anybody else seen this behavior before? Thanks. :confused: Sincerely Yours, Brian Hart Department of Physics and Astronomy University of California, Irvine

    N Offline
    N Offline
    Navin
    wrote on last edited by
    #2

    Where does the problem happen? Is it right at the delete[] arr; call, or is it later in the code? If it's later, chances are something else is trying to use it after it has been deleted. Otherwise, perhaps you are deleting arr somewhere else above, and not setting it to NULL, and then trying to delete it again? It's impossible to say without seeing the rest of the code. BTW, you are right in calling delete[]. "When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity." - Albert Einstein

    Brian C HartB 1 Reply Last reply
    0
    • Brian C HartB Brian C Hart

      Hello all, I use VC6. Haven't the funds to bother with VS.NET yet... I have the weirdest problems happening lately, with this code: double* arr = new double[some_length]; if (arr == NULL) return; /* validity check */ //...use arr here ... delete[] arr; /* segmentation fault here -- wtf??? */ arr = NULL; I have no idea what could be causing this -- I mean, I am behaving myself, and calling delete for arrays of a primitive type that I allocate on the heap, right? The problem is there whether I call either delete[] or just plain delete. The problem goes away when I remove the call to delete[] entirely. same thing happens using malloc()/free()...what's going on? This is bizzare. Has anybody else seen this behavior before? Thanks. :confused: Sincerely Yours, Brian Hart Department of Physics and Astronomy University of California, Irvine

      N Offline
      N Offline
      Neville Franks
      wrote on last edited by
      #3

      You haven't said precisely what the error is, but I assume it is an error in the heap. I think you'll find you've screwed the heap somewhere in your code and when delete(free) is called it is detecting this. There are various flags you can set to help track heap problems. Have a look at _CrtSetDbgFlag(). Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com

      Brian C HartB 1 Reply Last reply
      0
      • N Navin

        Where does the problem happen? Is it right at the delete[] arr; call, or is it later in the code? If it's later, chances are something else is trying to use it after it has been deleted. Otherwise, perhaps you are deleting arr somewhere else above, and not setting it to NULL, and then trying to delete it again? It's impossible to say without seeing the rest of the code. BTW, you are right in calling delete[]. "When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity." - Albert Einstein

        Brian C HartB Offline
        Brian C HartB Offline
        Brian C Hart
        wrote on last edited by
        #4

        Hello Navin, It faults right on delete[] arr;. I don't understand. Whenever I delete any pointer in my program, I always do: if (arr != NULL) { delete[] arr; arr = NULL; } However, it still faults on the delete[] arr;. I agree with Neville Franks that I think I am screwing the heap, but how does one prevent this? Thanks for any help -- it is much appreciated. Sincerely Yours, Brian Hart Department of Physics and Astronomy University of California, Irvine

        A 1 Reply Last reply
        0
        • Brian C HartB Brian C Hart

          Hello all, I use VC6. Haven't the funds to bother with VS.NET yet... I have the weirdest problems happening lately, with this code: double* arr = new double[some_length]; if (arr == NULL) return; /* validity check */ //...use arr here ... delete[] arr; /* segmentation fault here -- wtf??? */ arr = NULL; I have no idea what could be causing this -- I mean, I am behaving myself, and calling delete for arrays of a primitive type that I allocate on the heap, right? The problem is there whether I call either delete[] or just plain delete. The problem goes away when I remove the call to delete[] entirely. same thing happens using malloc()/free()...what's going on? This is bizzare. Has anybody else seen this behavior before? Thanks. :confused: Sincerely Yours, Brian Hart Department of Physics and Astronomy University of California, Irvine

          I Offline
          I Offline
          igor1960
          wrote on last edited by
          #5

          You are probably overruning arr To confirm: allocate one element more [some_length+1 ]; put some predefined double value into arr[some_length](1234.); before delete check that the value at arr[some_length] is still the same. If not -- you've got confirmation. Also, just a clue: if you do new and delete in different modules: check if both of them are using same runtime (Release/Debug). "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

          Brian C HartB 1 Reply Last reply
          0
          • Brian C HartB Brian C Hart

            Hello Navin, It faults right on delete[] arr;. I don't understand. Whenever I delete any pointer in my program, I always do: if (arr != NULL) { delete[] arr; arr = NULL; } However, it still faults on the delete[] arr;. I agree with Neville Franks that I think I am screwing the heap, but how does one prevent this? Thanks for any help -- it is much appreciated. Sincerely Yours, Brian Hart Department of Physics and Astronomy University of California, Irvine

            A Offline
            A Offline
            armentage
            wrote on last edited by
            #6

            Brian Hart wrote: if (arr != NULL) { delete[] arr; arr = NULL; } delete is smarter than the C free() function and does the right thing (which is to do nothing at all!) when you delete a pointer that is NULL (or 0). Ofcourse, still want to set your pointer to 0 when you are done.

            Brian C HartB J 2 Replies Last reply
            0
            • A armentage

              Brian Hart wrote: if (arr != NULL) { delete[] arr; arr = NULL; } delete is smarter than the C free() function and does the right thing (which is to do nothing at all!) when you delete a pointer that is NULL (or 0). Ofcourse, still want to set your pointer to 0 when you are done.

              Brian C HartB Offline
              Brian C HartB Offline
              Brian C Hart
              wrote on last edited by
              #7

              Hello All: I have solved the problem. What i have done is: double* arr = double[size]; ... /* fillall elements*/ delete[] arr; /* GP fault, sporadically */ arr = NULL; I discovered this goes away when I set the size of the array to size + 1; i.e. delete[] sometimes faults because the debug version of things told me that I was having buffer overruns. Setting the array size to size + n where n is some arbitary number gives the computer a 'buffer' to work with to protect agsinst overruns. Thanks for the help. Sincerely Yours, Brian Hart Department of Physics and Astronomy University of California, Irvine

              A 1 Reply Last reply
              0
              • N Neville Franks

                You haven't said precisely what the error is, but I assume it is an error in the heap. I think you'll find you've screwed the heap somewhere in your code and when delete(free) is called it is detecting this. There are various flags you can set to help track heap problems. Have a look at _CrtSetDbgFlag(). Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com

                Brian C HartB Offline
                Brian C HartB Offline
                Brian C Hart
                wrote on last edited by
                #8

                Neville Franks wrote: You haven't said precisely what the error is, but I assume it is an error in the heap. I think you'll find you've screwed the heap somewhere in your code and when delete(free) is called it is detecting this. There are various flags you can set to help track heap problems. Have a look at _CrtSetDbgFlag(). Hello Neville: I have solved the problem. It was just a buffer overrun, which I fixed by allocating a couple of more 'extra' elements than I needed. Thanks for the help. Sincerely Yours, Brian Hart Department of Physics and Astronomy University of California, Irvine

                1 Reply Last reply
                0
                • I igor1960

                  You are probably overruning arr To confirm: allocate one element more [some_length+1 ]; put some predefined double value into arr[some_length](1234.); before delete check that the value at arr[some_length] is still the same. If not -- you've got confirmation. Also, just a clue: if you do new and delete in different modules: check if both of them are using same runtime (Release/Debug). "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

                  Brian C HartB Offline
                  Brian C HartB Offline
                  Brian C Hart
                  wrote on last edited by
                  #9

                  igor1960 wrote: You are probably overruning arr To confirm: allocate one element more [some_length+1 ]; put some predefined double value into arr[some_length](1234.); before delete check that the value at ... Yep, that was it...thanks for the help. Sincerely Yours, Brian Hart Department of Physics and Astronomy University of California, Irvine

                  1 Reply Last reply
                  0
                  • Brian C HartB Brian C Hart

                    Hello All: I have solved the problem. What i have done is: double* arr = double[size]; ... /* fillall elements*/ delete[] arr; /* GP fault, sporadically */ arr = NULL; I discovered this goes away when I set the size of the array to size + 1; i.e. delete[] sometimes faults because the debug version of things told me that I was having buffer overruns. Setting the array size to size + n where n is some arbitary number gives the computer a 'buffer' to work with to protect agsinst overruns. Thanks for the help. Sincerely Yours, Brian Hart Department of Physics and Astronomy University of California, Irvine

                    A Offline
                    A Offline
                    armentage
                    wrote on last edited by
                    #10

                    Brian Hart wrote: I discovered this goes away when I set the size of the array to size + 1; i.e. delete[] sometimes faults because the debug version of things told me that I was having buffer overruns. Setting the array size to size + n where n is some arbitary number gives the computer a 'buffer' to work with to protect agsinst overruns. What you're describing here is a sever programming error. You're going past the end of your allocated 'arr' somewhere, and corrupting your stack. Your 'buffer' is just hiding the problem. You really should take a close look at your code, and figure out what's going wrong. You probablu have an array bounds problem. I would bet you're not even performing your calculation correctly. What you've said about "protecting against buffer overruns" is complete nonsense.

                    1 Reply Last reply
                    0
                    • A armentage

                      Brian Hart wrote: if (arr != NULL) { delete[] arr; arr = NULL; } delete is smarter than the C free() function and does the right thing (which is to do nothing at all!) when you delete a pointer that is NULL (or 0). Ofcourse, still want to set your pointer to 0 when you are done.

                      J Offline
                      J Offline
                      JT Anderson
                      wrote on last edited by
                      #11

                      Actually, free(NULL) is safe on any standard C compiler. There were certainly old, pre ANSI C, compilers that didn't handle free(NULL) well. -------- There are 10 types of people in this world. Those who know binary and those who don't.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups