Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 仅在版本生成时发生错误c0000374_C++_Visual Studio_C++11_Memory Management_Memory Leaks - Fatal编程技术网

C++ 仅在版本生成时发生错误c0000374

C++ 仅在版本生成时发生错误c0000374,c++,visual-studio,c++11,memory-management,memory-leaks,C++,Visual Studio,C++11,Memory Management,Memory Leaks,我为两个Dimensional对象数组的初始化值编写了函数: 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 &

我为两个Dimensional对象数组的初始化值编写了函数:

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;
}
类Cell是CellRgb的父级,两者都不分配其他数据,也不释放析构函数中的内存。 我不明白为什么我的代码会使程序崩溃。如果我内联createCells方法:

    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);
Mat-Mat=Mat();
单元格***红单元格=allocObjectArray(4,4);
对于(int y=0;y<4;++y)
{
对于(int x=0;x<4;++x)
{
红细胞[y][x]=新细胞RGB(&mat,2,x,y,0);
}
}
deleteObjectArray(红色单元格,4,4);
我的代码工作正常。 Mat是openCv库中的类。
我使用C++11和visual studio 2017社区开发我的应用程序。

你为什么要自己处理
新建
删除
,而不是使用标准的库设施?
T***
噢,天哪。为什么您觉得需要三层动态分配和间接寻址?@BoundaryPosition A三星级brogrammer;-)请不要试图成为一个@marcu,你说你是为android开发的,但是你用了最糟糕的方式分配了一个2d数组。你使用指针的方式不利于你努力做到“高效”。如果您阅读我的评论并点击链接,您会看到我给出的答案只需要2次分配,而与
高度
的值无关,而您当前的方法需要
高度
+1次分配,从而导致内存碎片化。
delete array[i][j];
    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);