0

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.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
marcu
  • 223
  • 4
  • 14
  • Why are you dealing with `new` and `delete` yourself, instead of using the standard library [Dynamic memory management](http://en.cppreference.com/w/cpp/memory) facilities? – πάντα ῥεῖ May 15 '17 at 18:22
  • 2
    `T***` Oh, my. Why do you feel you need _three_ layers of dynamic allocation and indirection? – Lightness Races in Orbit May 15 '17 at 18:23
  • 1
    @BoundaryImposition A _three star_ brogrammer ;-) – πάντα ῥεῖ May 15 '17 at 18:23
  • *in debug everything is ok* -- No it's not. – PaulMcKenzie May 15 '17 at 18:24
  • 3
    Please do not try to be a [three star programmer](http://wiki.c2.com/?ThreeStarProgrammer) – NathanOliver May 15 '17 at 18:24
  • @Nathan I sometimes have a feeling they think that's like receiving badges or extra stars at a uniform. – πάντα ῥεῖ May 15 '17 at 18:30
  • @OP -- Even if you did this using `new[]` and `delete[]` you seem to want **2-dimensional** data, not 3 dimensional data. If that's the case, you could eliminate one of the stars (thus being only a two-star programmer) by writing something [like this](http://stackoverflow.com/questions/21943621/how-to-create-a-contiguous-2d-array-in-c/21944048#21944048) – PaulMcKenzie May 15 '17 at 18:31
  • The error code usually indicates a corrupted heap, in which case you've broken something *somewhere* before the call to `deleteObjectArray`. – molbdnilo May 15 '17 at 18:46
  • Just gonna say, `std::vector>>` could probably eliminate a *ton* of this code. Maybe think about that. – WhozCraig May 15 '17 at 18:54
  • I don't use c++ often and i use T*** because i didn't know that is bad pattern and my classes doesn't have default constructor. It looked good for first sight. I use new and delete because i read about overhead of smart pointers. My code will work on android and i don't want use too much containers if they not really necessary. I ask this question because while developing i saw problem that was too hard to fix for me. @πάνταῥεῖ unnecessary last comment... Thanks for advices. I know what i should do to avoid this errors – marcu May 15 '17 at 22:02
  • 1
    @marcu You say you are developing for android, but you used the worst way to allocate a 2d array. The way you are using pointers goes against you trying to be "efficient". If you read my comment and followed the link, you see the answer I gave just requires 2 allocations regardless of the value of `height`, while your current approach requires `height` + 1 allocations, thus fragmenting the memory. – PaulMcKenzie May 16 '17 at 00:28

0 Answers0