Heap Corruption Error on delete
Hello Everyone,
I'm having problems deleting a new variable!
I have done exactly as is shown elsewhere on the website:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#define HEIGHT 5
#define WIDTH 3
#define DEPTH 7
int main() {
double ***p2DArray;
// Allocate memory
p2DArray = new double**[HEIGHT];
for (int i = 0; i < HEIGHT; ++i) {
p2DArray[i] = new double*[WIDTH];
for (int j = 0; j < WIDTH; ++j)
p2DArray[i][j] = new double[DEPTH];
}
// Assign values
p2DArray[0][0][0] = 3.6;
p2DArray[1][2][4] = 4.0;
// De-Allocate memory to prevent memory leak
for (int i = 0; i < HEIGHT; ++i) {
for (int j = 0; j < WIDTH; ++j)
delete [] p2DArray[i][j];
delete [] p2DArray[i];
}
delete [] p2DArray;
return 0;
}
|
The problem happens on the second iteration of the second for loop
(ie at delete [] p2DArray[i][j])
What is the problem?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
//Initialize picture temporary memory matrix
unsigned int *** picTemp = new unsigned int ** [cyScreen];
for ( unsigned int i = 0; i < cyScreen; i++ )
{
picTemp[i] = new unsigned int * [cxScreen];
for ( unsigned int j = 0; j < cxScreen; j++ )
picTemp [i] [j] = new unsigned int [3]; //3 layers for a picture
}
...
...
...
for ( int i = 0; i < cyScreen; i++ )
{
for ( int j = 0; j < cxScreen; j++ )
{
delete [] picTemp [i] [j];
}
delete [] picTemp [i];
}
delete [] picTemp;
|
In this case, cxScreen = 1280, cyScreen = 800.
Cheers,
Tom
Last edited on
Your first example looks and works fine for me.
Something can't be right... there are 2 new's and 3 delete's.
Memory is being mismanaged; there should be an equal number of both.
Last edited on
I count three new and deletes in both examples!
Whoops, missed line 2 in the second example... never mind :)
The corruption likely happens in the code between
newing and
deleteing:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include<iostream>
#define HEIGHT 10
#define WIDTH 10
#define DEPTH 3
int main() {
unsigned ***p2DArray;
// Allocate memory
p2DArray = new unsigned**[HEIGHT];
for (int i = 0; i < HEIGHT; ++i)
{
p2DArray[i] = new unsigned*[WIDTH];
for (int j = 0; j < WIDTH; ++j)
{
p2DArray[i][j] = new unsigned[DEPTH];
}
}
// Assign values
for (int i = 0; i < HEIGHT; ++i)
{
for(int j = 0; j < WIDTH; ++j)
{
p2DArray[i][j][0] = 1;
p2DArray[i][j][1] = 2;
p2DArray[i][j][2] = 3;
}
}
// This line will cause a heap corruption to be shown
// when delteing the memory
p2DArray[1][1][4] = 2;
// De-Allocate memory to prevent memory leak
for (int i = 0; i < HEIGHT; ++i)
{
for (int j = 0; j < WIDTH; ++j)
{
std::cout << i << ":" << j << std::endl;
delete [] p2DArray[i][j];
}
delete [] p2DArray[i];
}
delete [] p2DArray;
return 0;
}
|
NB: it will probable only show when debugging
Last edited on
Excellent, thank you guys.
I was looking for it but I skimmed over it so easily because I'm used to MatLab starting their indexing from 1!
In my main code I was accessing picTemp[i][j][3] sometimes. So I switcherooed that and its perfect now.
Thanks very much!
Tom
Topic archived. No new replies allowed.