4

I am allocating an array, and then when i call delete[] it causes the program to crash, the program works fine when I do not call delete. Here is my code

MyObject *myArray= new MyObject[numPoints];
delete[] myArray;

I'm super confused so any help would be appreciated

Also, when I debug I get the error message "HEAP CORRUPTION DETECTED: after Normal block (#48) at 0x000032E90. CRT detected that the application wrote to memory after end of heap buffer."

SOLUTION!: I was initializing the array with not enough space. For some reason I could still add things to the array but it would crash when the destructor was called.

James
  • 2,951
  • 8
  • 41
  • 55
  • 6
    The debug message is correct - something is messing with the management of local variables. You'll need to paste the rest of your code for people to see the error. But you may already have enough clues to find it yourself. The error is not in the code you posted. – tenfour Sep 20 '11 at 17:05
  • 1
    What is the value of `numPoints`? What does the constructor for `MyObject` look like? Obviously the problem is in some code that you are not showing here. EDIT: Ninja'd – Ed S. Sep 20 '11 at 17:06
  • 3
    The destructor of `MyObject` class might be messing around. – Desmond Hume Sep 20 '11 at 17:08
  • 1
    Try to isolate a small *complete program* that reproduces this. Doing that will help you figure out what is doing the nasties, and possibly even fix it yourself. – R. Martinho Fernandes Sep 20 '11 at 17:08
  • Do you pass the `myArray` to some other function, where it gets deleted? You are probably deleting `myArray` twice. – Alok Save Sep 20 '11 at 17:09
  • 1
    Post the complete program for better manipulation – shubhendu mahajan Sep 20 '11 at 17:17
  • Mark B probably has the correct answer. But you should really post a compilable version of your code that exhibits the error (as small as possible). Usually the processes of reducing the problem to its smallest reproducible piece of code will help you find the error. – Martin York Sep 20 '11 at 18:24
  • This may help: http://stackoverflow.com/questions/255612/c-dynamically-allocating-an-array-of-objects/255744#255744 – Martin York Sep 20 '11 at 18:26
  • 1
    Use `new float[2]`, you have two points, not one. – Frigo Sep 20 '11 at 23:17

3 Answers3

3

My psychic debugging powers tell me that since MyObject is doing dynamic allocation, you forgot to obey the rule of three....you're missing a copy constructor, copy assignment operator, or both. For one example see http://drdobbs.com/184401400

But since this is C++ you can solve all your problems by just using vector instead. Please strongly consider that approach.

Mark B
  • 95,107
  • 10
  • 109
  • 188
  • It is doing dynamic allocation of the array. Good guessing! how does do I solve this without using vectors? I don't fully understand what the copy constructor does – James Sep 20 '11 at 17:30
  • I put a link to a Dr. Dobb's article that will hopefully explain things. But why can't you use `vector`? – Mark B Sep 20 '11 at 17:34
  • Doesn't one normally get a different error message for double-deletion? (coming from violating the rule of three?) – Mooing Duck Sep 20 '11 at 18:27
  • @Mooing Duck : depends on what happens between the two deletions. – MSalters Sep 20 '11 at 20:06
  • @MarkB In some organizations the leads/architects just don't prefer vector or other STL related stuff. I am not speaking for James but, just saying my experience. – Jagannath Sep 20 '11 at 23:00
1

CRT detected that the application wrote to memory after end of heap buffer.
This usually means that you wrote past the end of the array.
Solution 1: find every single place you access the array, and put in an assert to validate that the index is greater than or equal to zero, and less than numPoints.
Solution 2: replace MyObject * with a std::vector. (Do this one)

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
1
EDIT: When i uncomment everythign in the MYObject deconstructor the program does not crash. The deconstructor code is :

delete [] myPoints;
points is an array in MyObject.

Wait what, you allocate an array of MyObjects in your MyObject constructor? It's no small wonder you run out of heap if you allocate arrays recursively. At least if I understand correctly and you meant destructor.

If this is not the case, something fishy is still going on in your MyObject class. Either in the constructor or operator new [] or operator delete [].

Frigo
  • 1,709
  • 1
  • 14
  • 32
  • no, points is an array INSIDE MyObject, it is not ad array OF MyObjects. It is an array of floats – James Sep 20 '11 at 22:52
  • 1
    Ah, okay. Makes sense now. Do you modify myPoints somewhere in your class, or share it with other instances? – Frigo Sep 20 '11 at 23:00