I written functions for initialization values of two-dimmensional object array:
template <typename T>
T*** allocObjectArray(const int& height, const int& width)
{
std::cout << "alloc" << std::endl; //todo remove
T*** array = new T**[height];
for (int i = 0; i < height; ++i)
{
array[i] = new T*[width];
}
return array;
}
void createCells(Cell*** cells, int channels)
{
std::cout << "create cells" << std::endl; //todo remove
for (int y = 0; y < cellsVertical; ++y)
{
for (int x = 0; x < cellsHorizontal; ++x)
{
cells[y][x] = new CellRgb(image, cellSize, x, y, channels);
}
}
}
template <typename T>
void deleteObjectArray(T*** array, const int height, const int width)
{
std::cout << "delete" << std::endl; //todo remove
if (array == nullptr)
{
return;
}
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
delete array[i][j];
}
delete[] array[i];
}
delete[] array;
}
usage:
Cell*** redCells = allocObjectArray<Cell>(cellsVertical, cellsHorizontal);
createCells(redCells, 0);
deleteObjectArray(redCells, cellsVertical, cellsHorizontal);
But always when i run my code as release (in debug everything is ok) i get error error c0000374 in deleteObjectArray method in line:
delete array[i][j];
Class Cell is parent of CellRgb and both don't alloc other data and don't free memory in destructors. I don't understand why my code crash program. If i inline createCells method:
Mat mat = Mat();
Cell*** redCells = allocObjectArray<Cell>(4, 4);
for (int y = 0; y < 4; ++y)
{
for (int x = 0; x < 4; ++x)
{
redCells[y][x] = new CellRgb(&mat, 2, x, y, 0);
}
}
deleteObjectArray(redCells, 4, 4);
my code works properly. Mat is class from openCv library. I develop my app using C++11 and visual studio 2017 community.