Introduction to qscopedpointer

Source: Internet
Author: User
Introduction to qscopedpointer Source: http://labs.qt.nokia.com/2009/08/21/introducing-qscopedpointer/by Harald fernengel on August 21,2009
Sorry for the first translation. Qt usually takes the boring memory allocation and deallocation from you, either through its implicitly shared containers, or with qobject's parent child relationship model. but every once in a while, we need to allocate something on the heap, and then the stress starts-where do we delete it, and how do we make sure to not leak the memory?
Generally, QT takes over boring garbage collection from C ++ developers, such as using QT's own implicit sharing or using the qobject parent-child relationship model. However, every time we need to allocate memory on the stack, there is pressure-where do we need to delete it? How can we ensure that there will be no memory leakage?
To fix this problem, qscopedpointer was born. It will delete the object it is pointing to automatically when it goes out of scope: To solve this problem, qscopedpointer was born. When the scope is exceeded, qscopedpointer will automatically delete the object (memory) It points ): VoidFoo ()
{
Qscopedpointer <Int> I (NewInt(42 ));
//...
If(Somecondition)
Return;
// An integer allocated on the heap will be in this position,
//...
} // Or this location is automatically deleted
A new exit condition in our function will not make it leak the integer that we allocated. In our function, a new exit condition will keep our allocated memory from being leaked! (Never translate)
So how do we access the object that we are pointing? Qscopedpointer implements operator * and operator->, So you it can be accessed just like any other pointer: So, how can we access and use the objects pointed to by qscopedpointer? Qscopedpointer has implemented operator * and operator-> Operator overloading, so use it like a normal pointer: qscopedpointer < Int> I ( New Int(42 ));
* I =
43;
Some operators are missing by design, for example the assignment operator: Some Common Operations (Operator overloading) are intentionally not implemented in design, such as the value assignment operator (=); qscopedpointer < Int> I ( New Int(42 ));
I = New Int(43); // Error
I. Reset ( New
Int(43 ));
// Correct
We figured that "Reset" looks scary enough to make the reader realize that the old object is deleted, and the qscopedpointer is now pointing to the new object. we imagine that "reset" looks scary enough to make readers realize that this will delete the old object and point qscopedpointer to the new object.
Another operator that is missing by design is the operator T * () that wowould allow accessing the pointer directly. this prevents accidents like: another operator that is not intended to be implemented at the design stage is T * (), which causes direct access to our pointers instead of pointer-managed objects. This is used to prevent the following situations: Int* Foo ()
{
Qscopedpointer < Int> I ( New Int(42 ));
//...
ReturnI;
// Thankfully, this does not compile.
}
Do you see the mistake? The moment we return, our object will be deleted, because the scoped pointer goes out of scope. we wowould return a dangling pointer, potentially leading to a nasty crash. however, we can tell qscopedpointer that its job is done and that we take ownership of the heap object by calling
Take (). Our function might look like this: Have you seen any errors? When the function returns, our object will be deleted because qscopedpointer leaves its scope. We will return a suspended pointer, which may cause program crash. Then, when we can call the take () member function and tell qscopedpointer that its work has been completed to take over control of the heap to which it points, our function may look like the following: Int* Foo ()
{
Qscopedpointer < Int> I ( New Int(42 ));
...
If(Someerror)
Return
0; // Our integer is deleted here
ReturnI. Take ();
// From now on, our heap object is on its own.
}
But what about memory allocated with malloc, or the operator new [] for arrays? For those cases, we introduced a second template parameter to qscopedpointer that defines the cleanup: But what if the memory is allocated through malloc or the array allocated through new? In this case, we introduced the second template parameter for qscopedpointer to define its cleanup job: qscopedpointer < Int, Qscopedpointerpoddeleter> POD ( Static_cast< Int*> (Malloc ( Sizeof Int)));
Qscopedpointerpoddeleter (POD stands for "plain old data") will call free on the object if our qscopedpointer goes out of scope. for convenience, there is a qscopedarraypointer that defaults to deleting the object it is pointing to with the Delete [] operator. it also features operator [] for convenience, so we can write: when our qscopedpointer is out of its scope, qscopedpointerpoddeleter (POD stands for normal old data (plain old data) will The object to which it points calls the free () function. For convenience, we use a qscopedarraypointer to point to the objects that need to be deleted by Delete. For convenience, it also has operator [] OPERATOR overload (take array members), so we can write like this: VoidFoo ()
{
Qscopedarraypointer < Int> I ( New Int[10]);
I [2] =
42;
...
Return;
// Our integer array is now deleted using Delete []
}
Note that if you have a reference counted object, you can use qexplicitlyshareddatapointer to ensure that an object is deleted correctly when its reference count goes to 0. note: If you need to manage objects that use the reference counter technology, you can use qexplicitlysharedatapointer to manage these objects, so that they can be deleted correctly when the reference count is changed to 0.
Qscopedpointer and qexplicitlyshareddatapointer are already used all over the place in the QT for s60 branch and will soon hit qt's master branch. and the best part is that with the introduction of these smart pointers, We cocould remove lots of boring code and make QT more readable without adding overhead-since all functions are inlined,
The resulting binary using qscopedpointer is identical to the manual new/delete approach. qscopedpointer and qexplicitlyshareddatapointer have been used in various parts of the s60 branch of QT development. Soon, it will enter the main development branch of QT. The biggest highlight is that QT's introduction of these smart pointers allows us to remove a lot of tedious code, and make QT more readable without increasing the overhead-because all functions are implemented inline at the same time, the binary file generated when qscopedpointer is used to manage the memory is the same as the manual new/delete method.
Happy developing can be developed with the smart pointer of QT.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.