清除图像地图
clearing a map of images
我有一个地图,其中键值是二维指针的地址,值是有关图像的一些元数据。
当我在分配和释放函数上运行 valgrind 时,valgrind 显示无效读取 4 错误。
struct MemType
{
// Store the data type of the pointer
int dataType;
int noOfRows;
int noOfColumns;
int noOfItems;
};
map < unsigned long, MemType > _MemHandle2DPointer;
short** AllocateMemory(int rowSize, int columnSize)
{
short** ptr2D = new short*[rowSize];
for (unsigned int i = 0; i < rowSize; i++)
{
ptr2D[i] = new short[columnSize];
//Initialize the memory
for(unsigned int j = 0; j < columnSize; j++)
{
ptr2D[i][j] = 0;
}
}
// Assign type id and add to the list of 2D pointers
MemType mem2DType;
mem2DType.dataType = 0;
// Store the number of rows and columns
mem2DType.noOfRows = rowSize;
mem2DType.noOfColumns = columnSize;
mem2DType.noOfItems = 0;
// Insert the pointer into the map
_MemHandle2DPointer[(long) ptr2D] = mem2DType;
return ptr2D;
}
void ReleaseMemory (short** ptr2D)
{
// Releasing memory occupied by 2D pointer
if (ptr2D != NULL)
{
map < unsigned long, MemType >::iterator iter = _MemHandle2DPointer.find((long)ptr2D);
if (iter != _MemHandle2DPointer.end())
{
//cout<<" Releasing Memory occupied by 2D pointer n";
_MemHandle2DPointer.erase((long)ptr2D);
for (unsigned int i = 0; i < iter->second.noOfRows; i++)
{
delete [] (short *) ptr2D[i];
}
delete [] (short **) ptr2D;
ptr2D = NULL;
}
}
}
int main()
{
short** dminImage = AllocateMemory(100,200);
ReleaseMemory (dminImage);
return 0;
}
我用这个解决方法解决了这个问题。
void ReleaseMemory (short** ptr2D){
// Releasing memory occupied by 2D pointer
if (ptr2D != NULL)
{
map < unsigned long, MemType >::iterator iter = _MemHandle2DPointer.find((long)ptr2D);
if (iter != _MemHandle2DPointer.end())
{
//cout<<" Releasing Memory occupied by 2D pointer n";
// _MemHandle2DPointer.erase((long)ptr2D);
for (unsigned int i = 0; i < iter->second.noOfRows; i++)
{
delete [] (short *) ptr2D[i];
}
_MemHandle2DPointer.erase((long)ptr2D);
delete [] (short **) ptr2D;
ptr2D = NULL;
}
}
}
我对导致早期错误的原因感到困惑。没有规定使用智能指针或任何 c++ 11 的东西,这是一个遗留代码。
您正在擦除具有迭代器的映射元素,然后仍然使用迭代器。别这样。迭代器通过擦除它指向的元素而失效。
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 为什么不;名字在地图上是按顺序排列的吗
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 基于多个条件处理地图中的所有元素
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 在C++中将矢量转换为嵌套地图
- 平均图像时图像损坏
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- Vulkan验证层不断在VkQueuePresentKHR()上抛出图像布局错误
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- 将RGB图像保存为PPM格式
- 合并图像的两面,一侧是 alpha 地图
- 清除图像地图
- 谷歌静态地图图像不在Tizen设备上工作,但在模拟器上
- Cocos2d-xTMX地图与游戏精灵共享纹理图像文件
- 如何在世界地图图像上突出显示特定国家
- (c++,Qt,Linux)将游戏地图线转换为图像线
- 将航拍图像拼接成地图