Merge pull request #1149 from jbj/resource-not-released-in-destructor-Qt

C++: Fix special-casing of Qt library in resource-not-released-in-destructor
This commit is contained in:
Geoffrey White
2019-03-21 16:13:25 +00:00
committed by GitHub
2 changed files with 26 additions and 1 deletions

View File

@@ -267,7 +267,7 @@ predicate automaticallyReleased(Assignment acquire)
{
// sub-types of the Qt type QObject are released by their parent (if they have one)
exists(NewExpr alloc |
alloc.getType() = qtObject() and
alloc.getAllocatedType() = qtObject() and
acquire.getRValue() = alloc and
alloc.getInitializer() = qtParentConstructor().getACallToThisFunction()
)

View File

@@ -0,0 +1,25 @@
struct QObject {
QObject(QObject *parent);
void setParent(QObject *parent);
};
struct DerivedFromQObject : public QObject {
DerivedFromQObject(QObject *parent);
};
class MyQtUser {
DerivedFromQObject *noParent, *constructorParent, *laterParent;
MyQtUser(QObject *parent) {
// This object sets its parent pointer to null and thus must be deleted
// manually.
noParent = new DerivedFromQObject(nullptr); // BAD [NOT DETECTED]
// This object does not need to be deleted because it will be deleted by
// its parent object when the time is right.
constructorParent = new DerivedFromQObject(parent); // GOOD
laterParent = new DerivedFromQObject(nullptr); // GOOD
laterParent->setParent(parent);
}
};