Line data Source code
1 : #include "FangObject.h"
2 :
3 : #include <QMutexLocker>
4 : #include <QDebug>
5 : #include <QMap>
6 :
7 : QSet<FangObject*> FangObject::allObjects;
8 :
9 3001 : FangObject::FangObject(QObject *parent) :
10 3001 : QObject(parent)
11 : {
12 : #ifdef FANG_OBJECT_LEAK_CHECK
13 3001 : QMutexLocker locker(&mutex);
14 3001 : allObjects.insert(this);
15 : #endif
16 3001 : }
17 :
18 2970 : FangObject::~FangObject()
19 : {
20 : #ifdef FANG_OBJECT_LEAK_CHECK
21 2949 : QMutexLocker locker(&mutex);
22 2949 : allObjects.remove(this);
23 : #endif
24 2970 : }
25 :
26 0 : void FangObject::printRemainingObjects()
27 : {
28 : #ifdef FANG_OBJECT_LEAK_CHECK
29 0 : qInfo();
30 0 : qInfo() << "========== FangObject leak check ==========";
31 0 : qInfo() << "Remaining objects: " << allObjects.size();
32 0 : qInfo();
33 :
34 : // Key: Class name
35 : // Value: Number of instances
36 0 : QMap<QString, int> instanceCount;
37 :
38 : // Tally up the totals.
39 0 : for (FangObject* object : allObjects) {
40 0 : instanceCount[object->metaObject()->className()]++;
41 : }
42 :
43 : // Print 'em out!
44 0 : for (QString className : instanceCount.keys()) {
45 0 : qDebug() << QString("%1 : %2").
46 0 : arg(instanceCount[className], 4).arg(className);
47 0 : }
48 :
49 0 : qInfo() << "===========================================";
50 0 : qInfo();
51 : #endif
52 0 : }
53 :
54 : #ifdef FANG_OBJECT_LEAK_CHECK
55 28 : int FangObject::getRemainingObjectCount()
56 : {
57 28 : return allObjects.size();
58 : }
59 : #endif
|