在学习Qt中看到这样的一个用法:
QScopedPointer app(new QApplication(argc, argv));看了一下帮助文档QScopedPointer的说明,整理记录一下
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon destruction
QScopedPointer 类存储动态申请的指针对象,并且在析构的时候自动释放指针
QScopedPointer guarantees that the object pointed to will get deleted when the current scope disappears. QScopedPointer 确保指针指向的对象能够自动释放
基本用法
不使用QScopedPointer 的常规方法,需要在很多地方手动调用delete,容易遗漏导致内存泄漏等问题
示例:
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;
}
使用QScopedPointer 类,程序能简化:
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);
}
const指针的QScopedPointer 用法
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 p(new QWidget()); -
QWidget *const p = new QWidget();
// is equivalent to:
const QScopedPointer p(new QWidget()); -
const QWidget *p = new QWidget();
// is equivalent to:
QScopedPointer p(new QWidget());
自定义cleanup handlers
QScopedPointer’s second template parameter can be used for custom cleanup handlers.
QScopedPointer’s的第二个模板参数可以用于cleanup handlers
The following custom cleanup handlers exist: 已有的cleanup handler
- QScopedPointerDeleter
the default, deletes the pointer using delete
示例:
// this QScopedPointer deletes its data using the delete[] operator:
QScopedPointer<int, QScopedPointerArrayDeleter > arrayPointer(new int[42]); - 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().
示例:
// this QScopedPointer frees its data using free():
QScopedPointer<int, QScopedPointerPodDeleter> podPointer(reinterpret_cast<int *>(malloc(42))); - QScopedPointerDeleteLater
deletes a pointer by calling deleteLater() on it. Use this handler for pointers to QObject’s that are actively participating in a QEventLoop. - 自定义cleanup handlers
如果想要使用自己定义的cleanup handlers,只需要其有一个public static函数void cleanup(T *pointer).
示例:
// this struct calls "myCustomDeallocator" to delete the pointer
struct ScopedPointerCustomDeleter //自定义的cleanup handler
{
static inline void cleanup(MyCustomClass *pointer) //需要包含一个cleanup(T *)p)的函数
{
myCustomDeallocator(pointer);
}
};
// QScopedPointer using a custom deleter:
QScopedPointer<MyCustomClass, ScopedPointerCustomDeleter> customPointer(new MyCustomClass);
前置申明的类使用QScopedPointer
Classes that are forward declared can be used within QScopedPointer
条件是无论何时,QScopedPointer需要cleanup的时候,前置申明的的类的析构函数能够使用
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 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. */
};
本文详细介绍了Qt中的QScopedPointer类,如何通过它自动管理动态分配的内存,避免内存泄漏,并展示了如何在不同场景下使用,包括const指针和自定义清理处理。

3092

被折叠的 条评论
为什么被折叠?



