In p = new Fred(), does the Fred memory “leak” if the Fred constructor throws an exception?
No.
If an exception occurs during the Fred constructor of p = new Fred(), the C++ language guarantees that the memory sizeof(Fred) bytes that were allocated will automagically be released back to the heap.
Here are the details: new Fred() is a two-step process:
sizeof(Fred)bytes of memory are allocated using the primitivevoid* operator new(size_t nbytes). This primitive is similar in spirit tomalloc(size_t nbytes). (Note, however, that these two are not interchangeable; e.g., there is no guarantee that the two memory allocation primitives even use the same heap!).- It constructs an object in that memory by calling the
Fredconstructor. The pointer returned from the first step is passed as thethisparameter to the constructor. This step is wrapped in atry…catchblock to handle the case when an exception is thrown during this step.
Thus the actual generated code is functionally similar to:
// Original code: Fred* p = new Fred();Fred* p;void* tmp = operator new(sizeof(Fred));try {new(tmp) Fred(); // Placement newp = (Fred*)tmp; // The pointer is assigned only if the ctor succeeds}catch (...) {operator delete(tmp); // Deallocate the memorythrow; // Re-throw the exception}
The statement marked “Placement new” calls the Fred constructor. The pointer p becomes the this pointer inside the constructor, Fred::Fred().
In p = new Fred(), does the Fred memory “leak” if the Fred constructor throws an exception?的更多相关文章
- Android 内存管理 &Memory Leak & OOM 分析
转载博客:http://blog.csdn.net/vshuang/article/details/39647167 1.Android 进程管理&内存 Android主要应用在嵌入式设备当中 ...
- quartz集群报错but has failed to stop it. This is very likely to create a memory leak.
quartz集群报错but has failed to stop it. This is very likely to create a memory leak. 在一台配置1核2G内存的阿里云服务器 ...
- 山东省第七届ACM省赛------Memory Leak
Memory Leak Time Limit: 2000MS Memory limit: 131072K 题目描述 Memory Leak is a well-known kind of bug in ...
- caching redirect views leads to memory leak (Spring 3.1)
在Spring 3.1以及以下版本使用org.springframework.web.servlet.view.UrlBasedViewResolver + cache(如下配置),在会出现任意种re ...
- 一则JVM memory leak解决的过程
起因是我们的集群应用(3台机器)新版本测试过程中,一般的JVM内存占用 都在1G左右, 但在运行了一段时间后,慢慢升到了4G, 这是一个明显不正常的现象. 定位 过程: 1.先在该机器上按照步骤尝试重 ...
- Linux C/C++ Memory Leak Detection Tool
目录 . 内存使用情况分析 . 内存泄漏(memory leak) . Valgrind使用 1. 内存使用情况分析 0x1: 系统总内存的分析 可以从proc目录下的meminfo文件了解到当前系统 ...
- SilverLight - Memory Leak
There is a memory leak issue in current silverlight project. It occurs in the search function: the m ...
- A memory leak issue with WPF Command Binding
Background In our application, we have a screen which hosts several tabs. In each tab, it contains a ...
- quartzScheduler_Worker-1] but has failed to stop it. This is very likely to create a memory leak解决
01-Jul-2016 07:24:20.218 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 80 ...
随机推荐
- 【bzoj1057】棋盘制作
题意 给定\(n*m\)的棋盘,每个格子有0或1其中的一种颜色. 求一个最大的正方形,满足正方形内0和1相互间隔. 求一个最大的矩形,满足矩形内0和1相互间隔. \(n,m\leq 2000\) 分析 ...
- Laravel多对多简析
首先生成两张数据表,一般要实现两张数据表之间的联系要建立第三张表,如下 数据表生成之后,生成一些测试数据,接下来就对表article_tag表进行操作 在模型文件中声明两张表之间的关系: 测试数据:
- IOC Container(服务容器)的工作机制
IOC Container 是laravel的一个核心内容,有了IOC Container在Laravel的强大表现,我们可以在Laravel中实现很大程度的代码维护性.(文档我是看的懵逼懵逼的(*^ ...
- CMD和AMD探秘
踏上前端这条道路以来,我一直以为自己就是个娴熟的切图工,每天只需要做着重复的劳动,切图,做网站.然而,技术的发展是日新月异的,切图工早就面临淘汰.随着浏览器功能越来越完善,前端项目越来越大,代码越来越 ...
- hdu------(1757)A Simple Math Problem(简单矩阵快速幂)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu----(4545)魔法串(LCS)
魔法串 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submiss ...
- Java 集合系列 09 HashMap详细介绍(源码解析)和使用示例
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- SQL Server数据库(表的创建)
表的创建 1.创建列(字段):列名+类型 2.设置主键列:能够唯一表示一条数据 3.设置唯一键:设计--索引/键--添加--唯一键(选择列)--确定 唯一键的内容不能重复 4.外键关系:一张表(从表) ...
- 串口调试,提示the given port name does not start with COM/com异常解决办法,,发现是打印机在搞怪
串口测试时,用到串口,把打印机的拔下来,换上测试的,程序一打开就提示错误:the given port name does not start with COM/com or does not res ...
- 石子归并问题(nyoj737)
石子合并(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的 ...