1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 Intel Corporation. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | /* |
30 | * This file exists because tst_qsharedpointer.cpp is compiled with |
31 | * QT_SHAREDPOINTER_TRACK_POINTERS. That changes some behavior. |
32 | * |
33 | * Note that most of these tests may yield false-positives in debug mode, but |
34 | * they should not yield false negatives. That is, they may report PASS when |
35 | * they are failing, but they should not produce FAILs. |
36 | * |
37 | * The reason for that is because of C++'s One Definition Rule: the macro |
38 | * changes some functions and, in debug mode, they will not be inlined. At link |
39 | * time, the two functions would be merged. |
40 | */ |
41 | |
42 | #include <qsharedpointer.h> |
43 | #include <QtTest> |
44 | |
45 | #include "nontracked.h" |
46 | |
47 | // We can't name our classes Data and DerivedData: those are in tst_qsharedpointer.cpp |
48 | namespace NonTracked { |
49 | |
50 | class Data |
51 | { |
52 | public: |
53 | static int destructorCounter; |
54 | static int generationCounter; |
55 | int generation; |
56 | |
57 | Data() : generation(++generationCounter) |
58 | { } |
59 | |
60 | virtual ~Data() |
61 | { |
62 | if (generation <= 0) |
63 | qFatal(msg: "tst_qsharedpointer: Double deletion!" ); |
64 | generation = 0; |
65 | ++destructorCounter; |
66 | } |
67 | }; |
68 | int Data::generationCounter = 0; |
69 | int Data::destructorCounter = 0; |
70 | |
71 | class DerivedData: public Data |
72 | { |
73 | public: |
74 | static int derivedDestructorCounter; |
75 | int moreData; |
76 | DerivedData() : moreData(0) { } |
77 | ~DerivedData() { ++derivedDestructorCounter; } |
78 | }; |
79 | int DerivedData::derivedDestructorCounter = 0; |
80 | |
81 | |
82 | #ifndef QTEST_NO_RTTI |
83 | void dynamicCastFailureNoLeak() |
84 | { |
85 | Data::destructorCounter = DerivedData::derivedDestructorCounter = 0; |
86 | |
87 | // see QTBUG-28924 |
88 | QSharedPointer<Data> a(new Data); |
89 | QSharedPointer<DerivedData> b = a.dynamicCast<DerivedData>(); |
90 | QVERIFY(!a.isNull()); |
91 | QVERIFY(b.isNull()); |
92 | |
93 | a.clear(); |
94 | b.clear(); |
95 | QVERIFY(a.isNull()); |
96 | |
97 | // verify that the destructors were called |
98 | QCOMPARE(Data::destructorCounter, 1); |
99 | QCOMPARE(DerivedData::derivedDestructorCounter, 0); |
100 | } |
101 | |
102 | #endif |
103 | } // namespace NonTracked |
104 | |