错误c0000374仅在发布版本

时间:2017-05-15 18:19:46

标签: c++ visual-studio c++11 memory-management memory-leaks

我为二维对象数组的初始化值编写了函数:

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*** redCells = allocObjectArray<Cell>(cellsVertical, cellsHorizontal);
createCells(redCells, 0);
deleteObjectArray(redCells, cellsVertical, cellsHorizontal);

但总是当我运行我的代码作为发布时(在调试中一切正常)我在deleteObjectArray方法中得到错误错误c0000374:

delete array[i][j];

Class 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是openCv库的类。 我使用C ++ 11和visual studio 2017社区开发我的应用程序。

0 个答案:

没有答案