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:

  1. sizeof(Fred) bytes of memory are allocated using the primitive void* operator new(size_t nbytes). This primitive is similar in spirit to malloc(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!).
  2. It constructs an object in that memory by calling the Fred constructor. The pointer returned from the first step is passed as the this parameter to the constructor. This step is wrapped in a trycatch block to handle the case when an exception is thrown during this step.

Thus the actual generated code is functionally similar to:

  1. // Original code: Fred* p = new Fred();
  2. Fred* p;
  3. void* tmp = operator new(sizeof(Fred));
  4. try {
  5. new(tmp) Fred(); // Placement new
  6. p = (Fred*)tmp; // The pointer is assigned only if the ctor succeeds
  7. }
  8. catch (...) {
  9. operator delete(tmp); // Deallocate the memory
  10. throw; // Re-throw the exception
  11. }

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?的更多相关文章

  1. Android 内存管理 &Memory Leak & OOM 分析

    转载博客:http://blog.csdn.net/vshuang/article/details/39647167 1.Android 进程管理&内存 Android主要应用在嵌入式设备当中 ...

  2. 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内存的阿里云服务器 ...

  3. 山东省第七届ACM省赛------Memory Leak

    Memory Leak Time Limit: 2000MS Memory limit: 131072K 题目描述 Memory Leak is a well-known kind of bug in ...

  4. caching redirect views leads to memory leak (Spring 3.1)

    在Spring 3.1以及以下版本使用org.springframework.web.servlet.view.UrlBasedViewResolver + cache(如下配置),在会出现任意种re ...

  5. 一则JVM memory leak解决的过程

    起因是我们的集群应用(3台机器)新版本测试过程中,一般的JVM内存占用 都在1G左右, 但在运行了一段时间后,慢慢升到了4G, 这是一个明显不正常的现象. 定位 过程: 1.先在该机器上按照步骤尝试重 ...

  6. Linux C/C++ Memory Leak Detection Tool

    目录 . 内存使用情况分析 . 内存泄漏(memory leak) . Valgrind使用 1. 内存使用情况分析 0x1: 系统总内存的分析 可以从proc目录下的meminfo文件了解到当前系统 ...

  7. SilverLight - Memory Leak

    There is a memory leak issue in current silverlight project. It occurs in the search function: the m ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 3D正方体

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  2. robotframework笔记17

    执行测试用例 基本用法 机器人框架从命令行执行测试用例,和 最终的结果是,在默认情况下,一个 输出文件 以XML格式和一个HTML 报告 和 日志 . 执行后,可以组合和输出文件 否则 进行后期处理  ...

  3. Qt之QRoundProgressBar(圆形进度条)

    简述 QRoundProgressBar类能够实现一个圆形进度条,继承自QWidget,并且有和QProgressBar类似的API接口. 简述 详细说明 风格 颜色 字体 共有函数 共有槽函数 详细 ...

  4. 笔记6:winfrom连接sql server 进行数据交换

    今天的作业是用winfrom窗体做一个留言板,如图: 要求和数据库有查询和添加功能.下拉框里的值是直接获取数据库中的值 一.连接数据库,获取表中数据 //创建一个存数据的表 DataTable tab ...

  5. Android 反编译工具简介

    Android 反编译工具: 所需工具:1 apktool : 用于获取资源文件 2 dex2Jar : 用于将classes.dex转化成jar文件 2 jd-gui: 将jar文件转化成java文 ...

  6. Servlet的尾(yi)巴---filter ( 过滤器 )的小应用

    该,该,该.......,继之前说到的 Filter 现在用这个来做一个小小的应用---------->  铛,铛,铛,铛.....  ->_->      <丑逼的留言板&g ...

  7. ARM指令集(上)

    ADuC702x可以用两套指令集:ARM指令集和Thumb指令集.本小节介绍ARM指令集.在介绍ARM指令集之前,先介绍指令的格式. A.2.1  指令格式         (1)基本格式       ...

  8. 【新手练习】类似Path的按钮,

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Hibernate映射之实体映射

    1.使用@注解配置实体类 实体类一般有ID.普通属性.集合属性等,分别对应数据库的主键.普通列.外键.@注解配置中,实体类用@Entity注解,用@Table指定对应的数据表,用@Id配置主键,用@C ...

  10. DataTable 转成字符串数组

    private static string[] autoCompleteWordList = null; public string[] GetCompleteDepart(int count,str ...