So I have this 2d dynamic array which content I want to free when I am done with it. However I keep running into a heap corruption after the destructor. The code works fine (of course with memory leaks) if I comment out the destructor. (Visual Studio 2005)
FrameData::FrameData(int width, int height)
{
width_ = width;
height_ = height;
linesize[0] = linesize[1] = linesize[2] = linesize[3] = 0;
// Initialise the 2d array
// Note: uint8_t is used by FFMPEG (typedef unsigned char uint8_t)
red = new uint8_t* [height];
green = new uint8_t* [height];
blue = new uint8_t* [height];
for (int i=0; i < height; i++)
{
red[i] = new uint8_t [width];
green[i] = new uint8_t [width];
blue[i] = new uint8_t [width];
}
}
FrameData::~FrameData()
{
// Delete each column
for (int i=0; i < height_; i++)
{
delete[] ((uint8_t*) red[i]);
delete[] ((uint8_t*)green[i]);
delete[] ((uint8_t*)blue[i]);
}
// Final cleanup
delete[] red;
red = NULL;
delete[] green;
green = NULL;
delete[] blue;
blue = NULL;
}
I have no idea what is wrong with the code. The only another thing is somewhere else, I did this in a loop where the crash occurred
FrameData myFrame;
std::vector<FrameData> frames;
...snipped...
frames.push_back(myFrame);
This shouldn't be causing any problem, right? If I remember correct, push_back makes a copy instead of storing a pointer or a reference.
PS. Yes, I should use vectors. But I am not allowed to.
Additional Info:
The operator= and copy constructor are not defined. I guess that's a reason for the problem.