File: kis_memory_leak_tracker.cpp

package info (click to toggle)
koffice 1%3A2.2.1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 157,656 kB
  • ctags: 110,762
  • sloc: cpp: 889,358; xml: 22,758; ansic: 6,533; python: 5,224; perl: 2,771; sh: 1,805; yacc: 1,304; ruby: 1,219; sql: 720; lex: 455; makefile: 76
file content (234 lines) | stat: -rw-r--r-- 6,706 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 *  Copyright (c) 2010 Cyrille Berger <cberger@cberger.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kis_memory_leak_tracker.h"

#include <QMutex>

#include <kglobal.h>
#include "kis_debug.h"

#include "config-memory-leak-tracker.h"

// Those defines are used to ignore classes that are often leaked due to a KisPaintDevice leak
#define IGNORE_MEMENTO_ITEM
#define IGNORE_TILE

// Only linux support the memory leak tracker
#ifndef Q_OS_LINUX
#undef HAVE_MEMORY_LEAK_TRACKER
#endif

// Disable the memory leak tracker on release build
#ifdef NDEBUG
#undef HAVE_MEMORY_LEAK_TRACKER
#endif

// Common function
KisMemoryLeakTracker* KisMemoryLeakTracker::instance()
{
    K_GLOBAL_STATIC(KisMemoryLeakTracker, s_instance);
    return s_instance;
}

#ifdef HAVE_MEMORY_LEAK_TRACKER

#include <QHash>

#ifdef Q_OS_LINUX

struct BacktraceInfo {
    BacktraceInfo() : trace(0), size(0) {
    }
    ~BacktraceInfo() {
        delete[] trace;
    }
    void** trace;
    int size;
};

#define BACKTRACE_SIZE 10

#include <execinfo.h>

#ifdef HAVE_BACKTRACE_SUPPORT
#define MAKE_BACKTRACEINFO \
    BacktraceInfo* info = new BacktraceInfo; \
    info->trace = new void*[BACKTRACE_SIZE]; \
    int n = backtrace(info->trace, BACKTRACE_SIZE); \
    info->size = n;
#else
#define MAKE_BACKTRACEINFO \
    BacktraceInfo* info = 0;
#endif

struct WhatInfo {
    QHash<const void*, BacktraceInfo*> infos;
    QString name;
};

struct KisMemoryLeakTracker::Private {
    QHash<const void*, WhatInfo > whatWhoWhen;
    template<typename _T_>
    void dumpReferencedObjectsAndDelete(QHash<const _T_*, WhatInfo >&, bool _delete);
    QMutex m;
};

template<typename _T_>
void KisMemoryLeakTracker::Private::dumpReferencedObjectsAndDelete(QHash<const _T_*, WhatInfo >& map, bool _delete)
{
    QMutexLocker l(&m);
    for (typename QHash<const _T_*, WhatInfo >::iterator it = map.begin();
            it != map.end(); ++it) {
        errKrita << "Object " << it.key() << "(" << it.value().name << ") is still referenced by " << it.value().infos.size() << " objects:";
        for (QHash<const void*, BacktraceInfo*>::iterator it2 = it.value().infos.begin();
                it2 != it.value().infos.end(); ++it2) {
            errKrita << "Referenced by " << it2.key() << " at:";
#ifdef HAVE_BACKTRACE_SUPPORT
            BacktraceInfo* info = it2.value();
            char** strings = backtrace_symbols(info->trace, info->size);
            for (int i = 0; i < info->size; ++i) {
                errKrita << strings[i];
            }
            if (_delete) {
                delete info;
                it2.value() = 0;
            }
#else
            Q_UNUSED(_delete);
            errKrita << "Enable backtrace support in kis_memory_leak_tracker.cpp";
#endif
        }
        errKrita << "=====";
    }
}

KisMemoryLeakTracker::KisMemoryLeakTracker() : d(new Private)
{
}

KisMemoryLeakTracker::~KisMemoryLeakTracker()
{
    if (d->whatWhoWhen.isEmpty()) {
        dbgKrita << "No leak detected.";
    } else {
        errKrita << "****************************************";
        errKrita << (d->whatWhoWhen.size()) << " leaks have been detected";
        d->dumpReferencedObjectsAndDelete(d->whatWhoWhen, true);
        errKrita << "****************************************";
    }
    delete d;
}

void KisMemoryLeakTracker::reference(const void* what, const void* bywho, const char* whatName)
{
    QMutexLocker l(&d->m);
    if (whatName == 0 || ( strcmp(whatName, "PK13KisSharedData") != 0
#ifdef IGNORE_MEMENTO_ITEM
                           && strcmp(whatName, "PK14KisMementoItem") != 0
#endif
#ifdef IGNORE_TILE
                           && strcmp(whatName, "PK7KisTile") != 0
#endif
        ) ) {
        MAKE_BACKTRACEINFO
        d->whatWhoWhen[what].infos[bywho] = info;
        if (whatName) {
            d->whatWhoWhen[what].name = whatName;
        }
    }
}

void KisMemoryLeakTracker::dereference(const void* what, const void* bywho)
{
    QMutexLocker l(&d->m);
    if (d->whatWhoWhen.contains(what)) {
        QHash<const void*, BacktraceInfo*>& whoWhen = d->whatWhoWhen[what].infos;
        delete whoWhen[bywho];
        whoWhen.remove(bywho);
        if (whoWhen.isEmpty()) {
            d->whatWhoWhen.remove(what);
        }
    }
}

void KisMemoryLeakTracker::dumpReferences()
{
    errKrita << "****************************************";
    errKrita << (d->whatWhoWhen.size()) << " objects are currently referenced";
    d->dumpReferencedObjectsAndDelete(d->whatWhoWhen, false);
    errKrita << "****************************************";
}

void KisMemoryLeakTracker::dumpReferences(const void* what)
{
    QMutexLocker l(&d->m);
    if (!d->whatWhoWhen.contains(what)) {
        errKrita << "Object " << what << " is not tracked";
        return;
    }
    
    WhatInfo& info = d->whatWhoWhen[what];
    dbgKrita << "Object " << what << "(" << info.name << ") is still referenced by " << info.infos.size() << " objects:";
    for (QHash<const void*, BacktraceInfo*>::iterator it2 = info.infos.begin();
            it2 != info.infos.end(); ++it2) {
        dbgKrita << "Referenced by " << it2.key() << " at:";
#ifdef HAVE_BACKTRACE_SUPPORT
        BacktraceInfo* info = it2.value();
        char** strings = backtrace_symbols(info->trace, info->size);
        for (int i = 0; i < info->size; ++i) {
            dbgKrita << strings[i];
        }
#else
            dbgKrita << "Enable backtrace support in kis_memory_leak_tracker.cpp";
#endif
    }
    dbgKrita << "=====";
}

#else
#error "Hum, no memory leak tracker for your platform"
#endif

#else

KisMemoryLeakTracker::KisMemoryLeakTracker() : d(0)
{
}

KisMemoryLeakTracker::~KisMemoryLeakTracker()
{
}

void KisMemoryLeakTracker::reference(const void* what, const void* bywho, const char* whatName)
{
}

void KisMemoryLeakTracker::dereference(const void* what, const void* bywho)
{
}

void KisMemoryLeakTracker::dumpReferences()
{
}

void KisMemoryLeakTracker::dumpReferences(const void* what)
{
}

#endif