使用列表列表时发生内存泄漏

发布于 2024-11-10 11:27:14 字数 1112 浏览 0 评论 0原文

有人向我扔了一些代码来“生产化”。我运行了内存泄漏检查器,它在下面的“for”循环中调用了以下行作为内存泄漏。

someStruct->arrayMap = new std::list<BasisIndex>*[someStruct->mapSizeX];
for(int i=0; i<someStruct->mapSizeX; i++){  
    someStruct->arrayMap[i] = new std::list<BasisIndex>[someStruct->mapSizeY];
}

以下是数组映射的声明方式:

struct SomeStruct{
    int mapSizeX;
    int mapSizeY;
    std::list<BasisIndex>** arrayMap;
};

以下是它的一些用法:

someStruct->arrayMap[xVal][yVal].push_back(tempIndex);

for(it = someStruct->arrayMap[xVal][yVal].begin(); it != someStruct->arrayMap[xVal][yVal].end(); it++){
    ...
}

内存泄漏检查器在我杀死它之前转储了 5 分钟。然后我在清理例程中添加了以下代码,但它仍然转储出 150 个警告,所有警告都指向顶部 for 循环内的代码行。

for(int x=0; x<someStruct->mapSizeX; x++){
    for(int y=0; y<someStruct->mapSizeY; y++){
        someStruct->arrayMap[x][y].clear();
        someStruct->arrayMap[x][y].~list();
    }
}

std::list<BasisIndex> ** temp = someStruct->arrayMap;
delete temp;

如何彻底删除与该数组映射相关的内存?

I had some code thrown at me to 'productionize.' I ran a memory leak checker and it calls out the following line within the 'for' loop below as a memory leak.

someStruct->arrayMap = new std::list<BasisIndex>*[someStruct->mapSizeX];
for(int i=0; i<someStruct->mapSizeX; i++){  
    someStruct->arrayMap[i] = new std::list<BasisIndex>[someStruct->mapSizeY];
}

Here is how the array map is declared:

struct SomeStruct{
    int mapSizeX;
    int mapSizeY;
    std::list<BasisIndex>** arrayMap;
};

Here are a couple usages of it:

someStruct->arrayMap[xVal][yVal].push_back(tempIndex);

for(it = someStruct->arrayMap[xVal][yVal].begin(); it != someStruct->arrayMap[xVal][yVal].end(); it++){
    ...
}

The memory leak checker dumped for 5 minutes before I killed it. Then I added the following bit of code in a cleanup routine but it still dumps out 150 warnings all pointing to the line of code within the for loop at the top.

for(int x=0; x<someStruct->mapSizeX; x++){
    for(int y=0; y<someStruct->mapSizeY; y++){
        someStruct->arrayMap[x][y].clear();
        someStruct->arrayMap[x][y].~list();
    }
}

std::list<BasisIndex> ** temp = someStruct->arrayMap;
delete temp;

How do I completely delete the memory associated with this array map?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失退 2024-11-17 11:27:14

按照与分配对象相反的顺序释放对象。

分配:

someStruct->arrayMap = new std::list<BasisIndex>*[someStruct->mapSizeX];
for(int i=0; i<someStruct->mapSizeX; i++){  
    someStruct->arrayMap[i] = new std::list<BasisIndex>[someStruct->mapSizeY];
}

解除分配:

for (int i=0; i<someStruct->mapSizeX; i++){
    delete[] someStruct->arrayMap[i];
}
delete[] someStruct->arrayMap;

Deallocate the objects in the reverse order that you allocated them.

Allocation:

someStruct->arrayMap = new std::list<BasisIndex>*[someStruct->mapSizeX];
for(int i=0; i<someStruct->mapSizeX; i++){  
    someStruct->arrayMap[i] = new std::list<BasisIndex>[someStruct->mapSizeY];
}

Deallocation:

for (int i=0; i<someStruct->mapSizeX; i++){
    delete[] someStruct->arrayMap[i];
}
delete[] someStruct->arrayMap;
枯寂 2024-11-17 11:27:14

someStruct->arrayMap[x][y].~list(); <-- 不应手动调用析构函数。 (我什至不知道在没有首先使用placement new的情况下这样做是有效的......)您需要使用delete来代替。

someStruct->arrayMap[x][y].~list(); <-- You should not call the destructor manually. (I didn't even know it was valid to do it that way when placement new wasn't used first...) You need to use delete instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文