QScopedPointer
QScopedpointer
detailed description
the QScopedpointer class stores a pointer to a dynamically allocated object, and deletes it upon destruction.
managing heap allocated objects manually is hard and error prone, with the common result that code leaks memory and is hard to maintain. QScopedpointer is a small utility class that heavily simplifies this by assigning stack-based memory ownership to heap allocations, more generally called resource acquisition is initialization(raii).
QScopedpointer guarantees that the object pointed to will get deleted when the current scope disappears.
consider this function which does heap allocations, and has various exit points:
void myfunction(bool usesubclass)
{
myclass *p = usesubclass ? new myclass() : new mysubclass;
qiodevice *device = handsoverownership();
if (m_value > 3) {
delete p;
delete device;
return;
}
try {
process(device);
}
catch (...) {
delete p;
delete device;
throw;
}
delete p;
delete device;
}
it's encumbered by the manual delete calls. with QScopedpointer, the code can be simplified to:
void myfunction(bool usesubclass)
{
// assuming that myclass has a virtual destructor
QScopedpointer<myclass> p(usesubclass ? new myclass() : new mysubclass);
QScopedpointer<qiodevice> device(handsoverownership());
if (m_value > 3)
return;
process(device);
}
the code the compiler generates for QScopedpointer is the same as when writing it manually. code that makes use of delete are candidates for QScopedpointer usage (and if not, possibly another type of smart pointer such as QSharedPointer). QScopedpointer intentionally has no copy constructor or assignment operator, such that ownership and lifetime is clearly communicated.
The const qualification on a regular C++ pointer can also be expressed with a QScopedpointer:
const QWidget *const p = new QWidget();
// is equivalent to:
const QScopedpointer<const QWidget> p(new QWidget());
QWidget *const p = new QWidget();
// is equivalent to:
const QScopedpointer<QWidget> p(new QWidget());
const QWidget *p = new QWidget();
// is equivalent to:
QScopedpointer<const QWidget> p(new QWidget());
Custom Cleanup Handlers
Arrays as well as pointers that have been allocated with malloc must not be deleted using delete. QScopedpointer's second template parameter can be used for custom cleanup handlers.
The following custom cleanup handlers exist:
- QScopedpointerDeleter - the default, deletes the pointer using delete
- QScopedpointerArrayDeleter - deletes the pointer using delete []. Use this handler for pointers that were allocated with new [].
- QScopedpointerPodDeleter - deletes the pointer using free(). Use this handler for pointers that were allocated with malloc().
- QScopedpointerDeleteLater - deletes a pointer by calling deleteLater() on it. Use this handler for pointers to QObject's that are actively participating in a QEventLoop.
You can pass your own classes as handlers, provided that they have a public static function void cleanup(T *pointer).
// this QScopedpointer deletes its data using the delete[] operator:
QScopedpointer<int, QScopedpointerArrayDeleter<int> > arrayPointer(new int[42]);
// this QScopedpointer frees its data using free():
QScopedpointer<int, QScopedpointerPodDeleter> podPointer(reinterpret_cast<int *>(malloc(42)));
// this struct calls "myCustomDeallocator" to delete the pointer
struct ScopedPointerCustomDeleter
{
static inline void cleanup(MyCustomClass *pointer)
{
myCustomDeallocator(pointer);
}
};
// QScopedpointer using a custom deleter:
QScopedpointer<MyCustomClass, ScopedPointerCustomDeleter> customPointer(new MyCustomClass);
Forward Declared Pointers
Classes that are forward declared can be used within QScopedpointer, as long as the destructor of the forward declared class is available whenever a QScopedpointer needs to clean up.
Concretely, this means that all classes containing a QScopedpointer that points to a forward declared class must have non-inline constructors, destructors and assignment operators:
class MyPrivateClass; // forward declare MyPrivateClass
class MyClass
{
private:
QScopedpointer<MyPrivateClass> privatePtr; // QScopedpointer to forward declared class
public:
MyClass(); // OK
inline ~MyClass() {} // VIOLATION - Destructor must not be inline
private:
Q_DISABLE_COPY(MyClass) // OK - copy constructor and assignment operators
// are now disabled, so the compiler won't implicitely
// generate them.
};
Otherwise, the compiler output a warning about not being able to destruct MyPrivateClass.
Related Classed
- QSharedPointer.
QScopedPointer的更多相关文章
- Qt也有垃圾回收(通过QScopedPointer实现),下决心在项目里使用QScopedPointer,省了太多事情了,而且更安全!!
也谈Qt的垃圾回收 前几天在做代码审核的时候,Kai Uwe Broulik建议使用QScopedPointer来替代手工内存管理,使用后发觉确实节约了不少代码量,我的CHERRY可以延长寿命了!但是 ...
- Qt智能指针QPointer, QSharedDataPointer ,QSharedPointer,QWeakPointer和QScopedPointer
QPointer (4.0) 已经过时,可以被QWeakPointer所替代,它不是线程安全的. QSharedDataPointer (4.0) -- 提供对数据的COPY-ON-WRITE以及浅拷 ...
- QPointer更安全,QScopedPointer自动出范围就删除,QSharedDataPointer帮助实现隐式共享
http://blog.csdn.net/hai200501019/article/details/8474582http://blog.csdn.net/hai200501019/article/d ...
- Qt on Android 核心编程
Qt on Android 核心编程(最好看的Qt编程书!CSDN博主foruok倾力奉献!) 安晓辉 著 ISBN 978-7-121-24457-5 2015年1月出版 定价:65.00元 4 ...
- Qt 之 入门例程 (一)
以 “Hello Qt” 为例,介绍如何建立一个 Qt 工程 . 1 QLabel 例程 QLabel 继承自 QFrame (继承自 QWidget),主要用来显示文本和图片. 1.1 Hell ...
- Qt QObject
[1]Qt的QObject 1.测试代码如下: #include<QApplication> #include<QPushButton> #include<QDebug& ...
- QCustomplot使用分享(七) 层(完结)
一.分层绘制 一直说要讲2.0.0版本,但总是想把1.3.2版本拿出来比较一下,这篇文章也不例外.QCustomPlot2.0.0beta版本比1.3.2release版本有一个很大的改进那就是分层绘 ...
- QCustomplot使用分享(六) 坐标轴和网格线
一.概述 前边已经写了5篇对QCustomPlot的讲解,看过上述的几篇文章后,基本就能做一些简单的使用了,但是如果想要做到高度的控制图表,那么坐标轴将是很重要的一部分,因为坐标轴就是图表的一个参考系 ...
- Qt中单例模式的实现(4种方法)
最简单的写法: 12345 static MyClass* MyClass::Instance(){ static MyClass inst; return &inst;} 过去很长一段时间一 ...
随机推荐
- UOJ #54 时空穿梭 —— 计数+莫比乌斯反演+多项式系数
题目:http://uoj.ac/problem/54 10分还要用 Lucas 定理囧...因为模数太小了不能直接算... #include<cstdio> #include<cs ...
- Redis简单介绍与安装
Redis是一个开源,高级的键值存储和一个适用的解决方案,用于构建高性能,可扩展的Web应用程序. Redis有三个主要特点,使它优越于其它键值数据存储系统 - 1) Redis将其数据库完全保存在内 ...
- Jmeter接口压测小思路
1.压力接口测试分2种:一种是单场景,压一个接口:第二种是混合场景,多个有关联的接口.压测时间,一般场景都运行10-15分钟.如果是疲劳测试,可以压一天或一周,根据实际情况定. 2.压测前要明确压测功 ...
- php-PSR
<?php/** * 符合psr-1,2的编程实例 */ namespace Standard; // 顶部命名空间// 空一行use Test\TestClass;//use引入类 /** * ...
- native方法
看到虚拟机栈和本地方法栈的区别的时候有点疑惑,因为本地方法栈为虚拟机的Native方法服务.以下转载一篇关于native方法的介绍: http://blog.csdn.net/wike163/arti ...
- css loading 效果
.loading{ width:160px; height:56px; position: absolute; top:50%; left:50%; line-height:56px; color:# ...
- 项目中Map端数据处理不均匀性分析
Map任务的不均匀性 最近发现Map端数据越来越不均匀,而处理输入的数据,写到本地磁盘的数据量都差不多,我们随便拿出来两个attempt任务(当前map数量为64个),33和45,33的counter ...
- MyBatis的适用场景和生命周期
MyBatis使用场景 对比Hibernate和MyBatis是我们常见的话题,Hibernate作为常用的ORM框架,它使用起来简单易懂,对于SQL语言的封装,让对于SQL并不是很熟练的程序员也可以 ...
- ThinkPHP实例—实现登录验证
ThinkPHP 验证 本篇我们将运用商城实例讲解一下如何运用ThinkPHP做一个登录验证 我们的框架目录结构如下图所示: 其中 app 文件夹就是我们的应用文件夹 它的目录结构如下所示 其中 ...
- Windows下安装GCC
1.GCC编译器的选择 Windows下最常见的安装GCC的方式有两种:Cygwin和MinGW.本文主要介绍MinGW的安装配置. 2.下载MinGW 下载地址:http://sourceforge ...