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.

最新文章

  1. getSlotFromBufferLocked: unknown buffer: 0xf3d94ca0
  2. android免root兼容所有版本ui调试工具
  3. linux软件管理之------编译安装nginx服务器并手动编写自动化运行脚本
  4. 《DSP using MATLAB》示例Example4.9
  5. WCF-学习笔记概述之计算服务(1)
  6. android学习资料
  7. mysqladmin
  8. Android微信智能心跳方案
  9. ftk学习记录(button一片)
  10. 转:Selenium2.0 click()不生效的解决办法
  11. 新手如何理解JS面向对象开发?
  12. 【笔记】 laravel 的路由
  13. CLOUD添加自定义基础数据
  14. /bin/bash: /bin/java: Is a directory 解决
  15. 【Java深入研究】1、object类
  16. 题外话 -- windows10系统C盘空间变大 CPU莫名跑满
  17. 基于win32的windows画板程序
  18. django中数据库的相关操作
  19. 131.005 Unsupervised Learning - Cluster | 非监督学习 - 聚类
  20. 【BZOJ4598】[Sdoi2016]模式字符串 树分治+hash

热门文章

  1. C++关于变量初始化的琐记
  2. vs2012加载T4MVC模板
  3. JavaScript Promise启示录--(转)
  4. 一、Jetty介绍
  5. EF中创建、使用Oracle数据库的Sequence(序列)功能
  6. 手游[追忆之青]动画导演:2D动画制作技巧
  7. Window下MySql 5.6 安装后内存占用很高的问题
  8. Asp.net mvc 限制路由参数类型
  9. GO 功能注释
  10. congst与指针