本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ Map中存储动态指针时正确释放内存
原文链接:https://www.stubbornhuang.com/353/
发布于:2019年11月10日 19:27:35
修改于:2020年01月03日 8:53:59

#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
#include <iostream>
#include <map>
#include <string>
using namespace std;
class Example
{
public:
Example();
Example(int newIndex);
virtual ~Example();
int m_Index;
};
Example::Example()
{
}
Example::Example(int newIndex)
{
m_Index = newIndex;
}
Example::~Example()
{
}
int main()
{
std::map<std::string, Example*> MyTestMap;
for (unsigned int i=0;i<10;++i)
{
std::string tempStr = std::to_string(i);
Example* pExample = new Example(i);
MyTestMap[tempStr] = pExample;
}
std::map<std::string, Example*>::iterator iter = MyTestMap.begin();
while (iter != MyTestMap.end())
{
delete iter->second;
iter->second = NULL;
iter = MyTestMap.erase(iter);
}
MyTestMap.clear();
getchar();
_CrtDumpMemoryLeaks();
return 0;
}
当前分类随机文章推荐
- Windows - 使用类的成员函数作为Win32窗口消息回调处理函数WindowProc 阅读31次,点赞0次
- GCC/GG++中编译优化选项-O -O0 -O1 -O2 -O3 -Os -Ofast -Og -Oz各自的区别和作用 阅读137次,点赞0次
- C++ - 使用ffmpeg读取视频旋转角度并使用OpenCV根据旋转角度对视频进行旋转复原 阅读525次,点赞0次
- C++11 - std::string - stod/stof/stoi/stol/stold/stoll/stoul/stoull,由std::string转换为int/long/float/double等其他类型 阅读1492次,点赞0次
- C++11 - 使用std::codecvt进行字符编码转换需要注意的时间效率问题 阅读541次,点赞1次
- C++ - Windows和Linux系统下获取当前可执行程序的绝对路径 阅读137次,点赞0次
- C++ – 字节数组byte[]或者unsigned char[]与long long的相互转换 阅读194次,点赞0次
- C++ - linux编译C++代码出现error: use of deleted function std::atomic
::atomic(const std::atomic 阅读90次,点赞0次&) - C++11 - 使用std::thread::join()/std::thread::detach()方法需要注意的点 阅读1386次,点赞0次
- C++ - GBK编码下的全角字符转半角字符 阅读562次,点赞0次
全站随机文章推荐
- 资源分享 - Handbook of Digital Image Synthesis - Scientific Foundations of Rendering 英文高清PDF下载 阅读549次,点赞0次
- Pytorch - 检测CUDA、cuDNN以及GPU版本的Pytorch是否安装成功、GPU显存测试 阅读1266次,点赞1次
- 资源分享 - Effective Computational Geometry for Curves and Surfaces 英文高清PDF下载 阅读788次,点赞0次
- Unity - Color32[]转IntPtr 阅读1702次,点赞0次
- 资源分享 - 3D Graphics for Game Programming 英文高清PDF下载 阅读515次,点赞0次
- C++ – UTF8编码下的全角字符转半角字符 阅读475次,点赞0次
- WordPress - 修改WP Editor.md markdown编辑器插件从剪切板粘贴图片上传的清晰度和质量 阅读88次,点赞0次
- C++ - 得到字符串中某个字符串出现的个数 阅读2259次,点赞1次
- 资源分享 - OpenGL SuperBible - Comprehensive Tutorial and Reference (Fifth Edition) OpenGL蓝宝书第5版英文高清PDF下载 阅读814次,点赞0次
- 资源分享 - Beginning DirectX 11 Game Programming 英文高清PDF下载 阅读513次,点赞0次
评论
144