Problems with the combination of QT smart pointer and qobject object tree system (parent-child System)

Source: Internet
Author: User
Problems with the combination of QT smart pointer and qobject object tree system (parent-child System)

The smart pointer of QT was proposed at qt4.5 to enable QT applications Program This solution can avoid hard-coded Delete and Memory leakage. However, when I wrote a program, I found that it had frequent errors when combined with the qobject object tree system (parent-child system. It is recorded as a log to prevent other people from making mistakes.
Qt smart pointers mainly include qsharedpointer and qscopedpointer. Of course there are other classes, but they are rarely used. Qsharedpointer was introduced at qt4.5, while qscopedpointer was introduced at qt4.6. I think this is because QT developers saw boost or Loki's excellent research on smart pointers, but decide to make a set of powerful smart pointers by yourself. Qt's smart pointer style is similar to boost, providing a class that is literally understandable for each pointer for encapsulation. However, developers (including me) need to note that if the objects contained in it are subclasses inherited from qobject, they will have to worry about it. Once the smart pointer is destroyed during running, an error may occur, making people confused.
The reason is that qobject has its own object tree system (parent-child system). When it interacts with other qobject sub-classes, it will save the pointer of the other party to form a parent-child relationship, in the end, a qobject subclass pointer will form a powerful tree structure. When the father destroys the child, the child will be destroyed first (if the child is created on the stack through the new operator ). However, when the smart pointer retains the qobject subclass, it will automatically call its destructor, resulting in two de facto delete operations. At this time, the compiler will report an error.
For the familiar qscopedpointer, it is a good choice to use it among the members of the class, but because it retains the subclass of qobject, when this smart pointer interacts with other qobject sub-classes, it is inevitable that the other party will retain the value of the original pointer and enter the class destructor, qscopedpointer's original pointer value will be prior to release and set to "deleted" value 0 xfeeefeee. In this case, automatic cleaning through smart pointers may only cause running errors. In the source code of qscopedpointer. H, we can see that the cleanup static function of the qscopedpointerdeleter class does not carry the value of the pointer before the delete operation. Therefore, an error occurs when an Invalid Pointer is deleted.

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.