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
Fred
constructor. The pointer returned from the first step is passed as thethis
parameter to the constructor. This step is wrapped in atry
…catch
block 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 new
p = (Fred*)tmp; // The pointer is assigned only if the ctor succeeds
}
catch (...) {
operator delete(tmp); // Deallocate the memory
throw; // 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 ...
随机推荐
- H5网页播放器播不了服务器上的mp4视频文件
打开IIS,在功能视图里找到MIME类型菜单,打开该菜单后鼠标右键添加.mp4扩展名的MIME类型video/mp4 其他视频文件播放不了估计也得在IIS里添加对应的MIME类型(从服务器下载文件时也 ...
- div布局
Margin: Margin属性用于设置两个元素之间的距离. Padding: Padding属性用于设置一个元素的边框与其内容的距离. Clear: 使用Float属性设置一行有多个DIV后(多列) ...
- 纯css3代码写九宫格效果
主要用到css3中的transition和布局知识.代码如下 <!DOCTYPE html> <html lang="en"> <head> & ...
- python读取指定内存的内容
import ctypes as ct t = ct.string_at(0x211000, 20) # (addr, size) print t 最好不要用解释性语言来开发底层,一般用C.
- hust--------The Minimum Length (最短循环节)(kmp)
F - The Minimum Length Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...
- 修改Window的hosts文件提示“该文件被其他程序占用”解决方案
1.打开C:\Windows\System32\drivers\etc中的hosts 2.右键——>属性——>安全 3.在修改保存就可以了
- jquery添加的html元素按钮为什么不执行类样式绑定的click事件
代码举例: 更多按钮: <input type="button" class="addMore" id="addMore${issue.id } ...
- B+树索引和哈希索引的区别——我在想全文搜索引擎为啥不用hash索引而非得使用B+呢?
哈希文件也称为散列文件,是利用哈希存储方式组织的文件,亦称为直接存取文件.它类似于哈希表,即根据文件中关键字的特点,设计一个哈希函数和处理冲突的方法,将记录哈希到存储设备上. 在哈希文件中,是使用一个 ...
- PHP 页面编码声明方法详解(header或meta)
php的header来定义一个php页面为utf编码或GBK编码 php页面为utf编码 header("Content-type: text/html; charset=utf-8&quo ...
- ODBC 小例
#include "stdafx.h"#include <windows.h>#include <stdio.h>#include <iostream ...