本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – std::map – 存储动态指针时正确释放内存
原文链接:https://www.stubbornhuang.com/738/
发布于:2020年03月03日 14:21:21
修改于:2020年03月03日 15:09:04

code
#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;
}
当前分类随机文章推荐
- C++ - 将Unicode std::wstring字符串转换为Unicode std::string转义字符,类似于\uxxxx的形式 阅读1423次,点赞0次
- C++11/std::thread - 线程管理join/detach 阅读2247次,点赞0次
- C++11/std::thread - 线程的基本用法 阅读3181次,点赞0次
- C++11/std::thread - 可作为线程函数的几种方式总结 阅读3469次,点赞1次
- C++ - 只有在Debug模式下才使用std::cout输出调试日志,Release发布版本不输出调试日志 阅读4159次,点赞0次
- C++ - 最简单的将文本文件的内容一次性读取到std::string的方法 阅读4544次,点赞4次
- C++ - Windows和Linux系统下获取当前可执行程序的绝对路径 阅读1766次,点赞0次
- C++STL容器 - std::map查找元素与判断键值是否存在方法总结 count,find,contains,equal_range,lower_bound,upper_bound 阅读929次,点赞0次
- C++11/std::condition_variable - 生产者消费者模型 阅读2720次,点赞0次
- C++ - 字节数组byte[]或者unsigned char[]与long的相互转换 阅读796次,点赞0次
全站随机文章推荐
- Google Adsense - 从Google Adsense开通到第一个10美元我用了一年时间 阅读1946次,点赞2次
- 资源分享 - Designing the User Experience of Game Development Tools 英文高清PDF下载 阅读893次,点赞0次
- 资源分享 - 深度学习入门之Pytorch(廖星宇著)PDF下载 阅读3061次,点赞0次
- 资源分享 - Image Objects - An Archaeology of Computer Graphics 英文高清PDF下载 阅读1661次,点赞1次
- VTK以批量三维点坐标为中心(点云)绘制球体,可用于标识特征点或者是化学分子 阅读4516次,点赞0次
- Python3爬虫 - requests库的requests.exceptions所有异常详细说明 阅读5406次,点赞2次
- 如何回看PLTV/m3u8直播源 阅读384次,点赞0次
- 资源分享 - Ray Tracing Gems II - Next Generation Real-Time Rendering with DXR, Vulkan, and OptiX-Apress 英文高清PDF下载 阅读1867次,点赞0次
- C++11/std::shared_ptr - 循环引用问题 阅读4000次,点赞0次
- C++11 - 父类与子类相互包含的时候该如何正确的使用智能指针,防止循环引用 阅读2448次,点赞0次
评论
167