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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkDebugLeaks.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkDebugLeaks.h"
#include "vtkCriticalSection.h"
#include "vtkObjectFactory.h"
#include "vtkWindows.h"
#include <string>
static const char *vtkDebugLeaksIgnoreClasses[] = {
0
};
//----------------------------------------------------------------------------
// return 1 if the class should be ignored
static int vtkDebugLeaksIgnoreClassesCheck(const char* s)
{
int i = 0;
while(vtkDebugLeaksIgnoreClasses[i])
{
if(strcmp(s, vtkDebugLeaksIgnoreClasses[i]) == 0)
{
return 1;
}
i++;
}
return 0;
}
vtkStandardNewMacro(vtkDebugLeaks);
//----------------------------------------------------------------------------
// A hash function for converting a string to a long
static inline size_t vtkHashString(const char* s)
{
unsigned long h = 0;
for ( ; *s; ++s)
{
h = 5*h + *s;
}
return static_cast<size_t>(h);
}
//----------------------------------------------------------------------------
class vtkDebugLeaksHashNode
{
public:
vtkDebugLeaksHashNode()
{
this->Count = 1; // if it goes in, then there is one of them
this->Key = 0;
this->Next = 0;
}
void Print(std::string& os)
{
if(this->Count)
{
char tmp[256];
snprintf(tmp, 256, "\" has %i %s still around.\n",this->Count,
(this->Count == 1) ? "instance" : "instances");
os += "Class \"";
os += this->Key;
os += tmp;
}
}
~vtkDebugLeaksHashNode()
{
delete [] this->Key;
delete this->Next;
}
public:
vtkDebugLeaksHashNode *Next;
char *Key;
int Count;
};
//----------------------------------------------------------------------------
class vtkDebugLeaksHashTable
{
public:
vtkDebugLeaksHashTable();
vtkDebugLeaksHashNode* GetNode(const char* name);
void IncrementCount(const char *name);
unsigned int GetCount(const char *name);
int DecrementCount(const char* name);
void PrintTable(std::string &os);
int IsEmpty();
~vtkDebugLeaksHashTable()
{
for (int i = 0; i < 64; i++)
{
vtkDebugLeaksHashNode *pos = this->Nodes[i];
delete pos;
}
}
private:
vtkDebugLeaksHashNode* Nodes[64];
};
//----------------------------------------------------------------------------
vtkDebugLeaksHashTable::vtkDebugLeaksHashTable()
{
for (int i = 0; i < 64; i++)
{
this->Nodes[i] = NULL;
}
}
//----------------------------------------------------------------------------
void vtkDebugLeaksHashTable::IncrementCount(const char * name)
{
vtkDebugLeaksHashNode *pos = this->GetNode(name);
if(pos)
{
pos->Count++;
return;
}
vtkDebugLeaksHashNode *newpos = new vtkDebugLeaksHashNode;
newpos->Key = strcpy(new char[strlen(name)+1], name);
unsigned long loc = (static_cast<unsigned long>(vtkHashString(name)) & 0x03f0) / 16;
pos = this->Nodes[loc];
if (!pos)
{
this->Nodes[loc] = newpos;
return;
}
while (pos->Next)
{
pos = pos->Next;
}
pos->Next = newpos;
}
//----------------------------------------------------------------------------
vtkDebugLeaksHashNode* vtkDebugLeaksHashTable::GetNode(const char* key)
{
unsigned long loc = (static_cast<unsigned long>(vtkHashString(key)) & 0x03f0) / 16;
vtkDebugLeaksHashNode *pos = this->Nodes[loc];
if (!pos)
{
return NULL;
}
while ((pos) && (strcmp(pos->Key, key) != 0) )
{
pos = pos->Next;
}
return pos;
}
//----------------------------------------------------------------------------
unsigned int vtkDebugLeaksHashTable::GetCount(const char* key)
{
unsigned long loc = (static_cast<unsigned long>(vtkHashString(key)) & 0x03f0) / 16;
vtkDebugLeaksHashNode *pos = this->Nodes[loc];
if (!pos)
{
return 0;
}
while ((pos)&&(pos->Key != key))
{
pos = pos->Next;
}
if (pos)
{
return pos->Count;
}
return 0;
}
//----------------------------------------------------------------------------
int vtkDebugLeaksHashTable::IsEmpty()
{
int count = 0;
for(int i =0; i < 64; i++)
{
vtkDebugLeaksHashNode *pos = this->Nodes[i];
if(pos)
{
if(!vtkDebugLeaksIgnoreClassesCheck(pos->Key))
{
count += pos->Count;
}
while(pos->Next)
{
pos = pos->Next;
if(!vtkDebugLeaksIgnoreClassesCheck(pos->Key))
{
count += pos->Count;
}
}
}
}
return !count;
}
//----------------------------------------------------------------------------
int vtkDebugLeaksHashTable::DecrementCount(const char *key)
{
vtkDebugLeaksHashNode *pos = this->GetNode(key);
if(pos)
{
pos->Count--;
return 1;
}
else
{
return 0;
}
}
//----------------------------------------------------------------------------
void vtkDebugLeaksHashTable::PrintTable(std::string &os)
{
for(int i =0; i < 64; i++)
{
vtkDebugLeaksHashNode *pos = this->Nodes[i];
if(pos)
{
if(!vtkDebugLeaksIgnoreClassesCheck(pos->Key))
{
pos->Print(os);
}
while(pos->Next)
{
pos = pos->Next;
if(!vtkDebugLeaksIgnoreClassesCheck(pos->Key))
{
pos->Print(os);
}
}
}
}
}
//----------------------------------------------------------------------------
#ifdef VTK_DEBUG_LEAKS
void vtkDebugLeaks::ConstructClass(const char* name)
{
vtkDebugLeaks::CriticalSection->Lock();
vtkDebugLeaks::MemoryTable->IncrementCount(name);
vtkDebugLeaks::CriticalSection->Unlock();
}
#else
void vtkDebugLeaks::ConstructClass(const char*)
{
}
#endif
//----------------------------------------------------------------------------
#ifdef VTK_DEBUG_LEAKS
void vtkDebugLeaks::DestructClass(const char* p)
{
vtkDebugLeaks::CriticalSection->Lock();
// Due to globals being deleted, this table may already have
// been deleted.
if(vtkDebugLeaks::MemoryTable &&
!vtkDebugLeaks::MemoryTable->DecrementCount(p))
{
vtkDebugLeaks::CriticalSection->Unlock();
vtkGenericWarningMacro("Deleting unknown object: " << p);
}
else
{
vtkDebugLeaks::CriticalSection->Unlock();
}
}
#else
void vtkDebugLeaks::DestructClass(const char*)
{
}
#endif
//----------------------------------------------------------------------------
void vtkDebugLeaks::SetDebugLeaksObserver(vtkDebugLeaksObserver* observer)
{
vtkDebugLeaks::Observer = observer;
}
//----------------------------------------------------------------------------
vtkDebugLeaksObserver* vtkDebugLeaks::GetDebugLeaksObserver()
{
return vtkDebugLeaks::Observer;
}
//----------------------------------------------------------------------------
void vtkDebugLeaks::ConstructingObject(vtkObjectBase* object)
{
if (vtkDebugLeaks::Observer)
{
vtkDebugLeaks::Observer->ConstructingObject(object);
}
}
//----------------------------------------------------------------------------
void vtkDebugLeaks::DestructingObject(vtkObjectBase* object)
{
if (vtkDebugLeaks::Observer)
{
vtkDebugLeaks::Observer->DestructingObject(object);
}
}
//----------------------------------------------------------------------------
int vtkDebugLeaks::PrintCurrentLeaks()
{
#ifdef VTK_DEBUG_LEAKS
if(vtkDebugLeaks::MemoryTable->IsEmpty())
{
// Log something anyway, so users know vtkDebugLeaks is active/working.
cout << "vtkDebugLeaks has found no leaks.\n";
return 0;
}
// we must not use stringstream ior strstream due to problems with
// finalizers in MSVC
std::string leaks;
vtkDebugLeaks::MemoryTable->PrintTable(leaks);
#ifdef _WIN32
fprintf(stderr,"%s",leaks.c_str());
fflush(stderr);
int cancel=0;
if(getenv("DASHBOARD_TEST_FROM_CTEST") ||
getenv("DART_TEST_FROM_DART"))
{
// Skip dialogs when running on dashboard.
return 1;
}
std::string::size_type myPos = 0;
int count = 0;
std::string msg;
msg = "vtkDebugLeaks has detected LEAKS!\n";
while(!cancel && myPos != leaks.npos)
{
std::string::size_type newPos = leaks.find('\n',myPos);
if (newPos != leaks.npos)
{
msg += leaks.substr(myPos,newPos-myPos);
msg += "\n";
myPos = newPos;
myPos++;
}
else
{
myPos = newPos;
}
count++;
if (count == 10)
{
count = 0;
cancel = vtkDebugLeaks::DisplayMessageBox(msg.c_str());
msg = "";
}
}
if (!cancel && count > 0)
{
vtkDebugLeaks::DisplayMessageBox(msg.c_str());
}
#else
cout << "vtkDebugLeaks has detected LEAKS!\n";
cout << leaks.c_str() << endl;
#endif
#endif
return 1;
}
//----------------------------------------------------------------------------
#ifdef _WIN32
int vtkDebugLeaks::DisplayMessageBox(const char* msg)
{
#ifdef UNICODE
wchar_t *wmsg = new wchar_t [mbstowcs(NULL, msg, 32000)+1];
mbstowcs(wmsg, msg, 32000);
int result = (MessageBox(NULL, wmsg, L"Error",
MB_ICONERROR | MB_OKCANCEL) == IDCANCEL);
delete [] wmsg;
#else
int result = (MessageBox(NULL, msg, "Error",
MB_ICONERROR | MB_OKCANCEL) == IDCANCEL);
#endif
return result;
}
#else
int vtkDebugLeaks::DisplayMessageBox(const char*)
{
return 0;
}
#endif
//----------------------------------------------------------------------------
int vtkDebugLeaks::GetExitError()
{
return vtkDebugLeaks::ExitError;
}
//----------------------------------------------------------------------------
void vtkDebugLeaks::SetExitError(int flag)
{
vtkDebugLeaks::ExitError = flag;
}
//----------------------------------------------------------------------------
void vtkDebugLeaks::ClassInitialize()
{
#ifdef VTK_DEBUG_LEAKS
// Create the hash table.
vtkDebugLeaks::MemoryTable = new vtkDebugLeaksHashTable;
// Create the lock for the critical sections.
vtkDebugLeaks::CriticalSection = new vtkSimpleCriticalSection;
// Default to error when leaks occur while running tests.
vtkDebugLeaks::ExitError = 1;
vtkDebugLeaks::Observer = 0;
#else
vtkDebugLeaks::MemoryTable = 0;
vtkDebugLeaks::CriticalSection = 0;
vtkDebugLeaks::ExitError = 0;
vtkDebugLeaks::Observer = 0;
#endif
}
//----------------------------------------------------------------------------
void vtkDebugLeaks::ClassFinalize()
{
#ifdef VTK_DEBUG_LEAKS
// Report leaks.
int leaked = vtkDebugLeaks::PrintCurrentLeaks();
// Destroy the hash table.
delete vtkDebugLeaks::MemoryTable;
vtkDebugLeaks::MemoryTable = 0;
// Destroy the lock for the critical sections.
delete vtkDebugLeaks::CriticalSection;
vtkDebugLeaks::CriticalSection = 0;
// Exit with error if leaks occurred and error mode is on.
if(leaked && vtkDebugLeaks::ExitError)
{
exit(1);
}
#endif
}
//----------------------------------------------------------------------------
// Purposely not initialized. ClassInitialize will handle it.
vtkDebugLeaksHashTable* vtkDebugLeaks::MemoryTable;
// Purposely not initialized. ClassInitialize will handle it.
vtkSimpleCriticalSection* vtkDebugLeaks::CriticalSection;
// Purposely not initialized. ClassInitialize will handle it.
int vtkDebugLeaks::ExitError;
// Purposely not initialized. ClassInitialize will handle it.
vtkDebugLeaksObserver* vtkDebugLeaks::Observer;
|