Skip to content

Commit 37bd472

Browse files
authored
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
2 parents 313134c + 5528423 commit 37bd472

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

cpp/ql/src/jsf/4.10 Classes/AV Rule 79.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ predicate automaticallyReleased(Assignment acquire)
267267
{
268268
// sub-types of the Qt type QObject are released by their parent (if they have one)
269269
exists(NewExpr alloc |
270-
alloc.getType() = qtObject() and
270+
alloc.getAllocatedType() = qtObject() and
271271
acquire.getRValue() = alloc and
272272
alloc.getInitializer() = qtParentConstructor().getACallToThisFunction()
273273
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
struct QObject {
2+
QObject(QObject *parent);
3+
void setParent(QObject *parent);
4+
};
5+
6+
struct DerivedFromQObject : public QObject {
7+
DerivedFromQObject(QObject *parent);
8+
};
9+
10+
class MyQtUser {
11+
DerivedFromQObject *noParent, *constructorParent, *laterParent;
12+
13+
MyQtUser(QObject *parent) {
14+
// This object sets its parent pointer to null and thus must be deleted
15+
// manually.
16+
noParent = new DerivedFromQObject(nullptr); // BAD [NOT DETECTED]
17+
18+
// This object does not need to be deleted because it will be deleted by
19+
// its parent object when the time is right.
20+
constructorParent = new DerivedFromQObject(parent); // GOOD
21+
22+
laterParent = new DerivedFromQObject(nullptr); // GOOD
23+
laterParent->setParent(parent);
24+
}
25+
};

0 commit comments

Comments
 (0)