VTK  9.4.20250131
vtkDebugLeaks.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
40#ifndef vtkDebugLeaks_h
41#define vtkDebugLeaks_h
42
43#include "vtkCommonCoreModule.h" // For export macro
44#include "vtkObject.h"
45
46#include "vtkDebug.h" // Needed for VTK_DEBUG_LEAKS macro setting.
47#include "vtkDebugLeaksManager.h" // Needed for proper singleton initialization
48
49#include <functional> // for finalizers
50#include <mutex> // for std::mutex
51#include <vector> // for finalizers
52
53VTK_ABI_NAMESPACE_BEGIN
54class vtkDebugLeaksHashTable;
55class vtkDebugLeaksTraceManager;
57
58class VTKCOMMONCORE_EXPORT vtkDebugLeaks : public vtkObject
59{
60public:
61 static vtkDebugLeaks* New();
62 vtkTypeMacro(vtkDebugLeaks, vtkObject);
63 void PrintSelf(ostream& os, vtkIndent indent) override;
64
68 static void ConstructClass(vtkObjectBase* object);
69
73 static void ConstructClass(const char* className);
74
78 static void DestructClass(vtkObjectBase* object);
79
83 static void DestructClass(const char* className);
84
89 static int PrintCurrentLeaks();
90
92
96 static int GetExitError();
97 static void SetExitError(int);
99
102
116 static void AddFinalizer(std::function<void()> finalizer);
117
118protected:
119 vtkDebugLeaks() = default;
120 ~vtkDebugLeaks() override = default;
121
122 static int DisplayMessageBox(const char*);
123
124 static void ClassInitialize();
125 static void ClassFinalize();
126
127 static void ConstructingObject(vtkObjectBase* object);
128 static void DestructingObject(vtkObjectBase* object);
129
130 static std::vector<std::function<void()>>* Finalizers;
131
133 friend class vtkObjectBase;
134
135private:
136 static vtkDebugLeaksHashTable* MemoryTable;
137 static vtkDebugLeaksTraceManager* TraceManager;
138 static std::mutex* CriticalSection;
139 static vtkDebugLeaksObserver* Observer;
140 static int ExitError;
141
142 vtkDebugLeaks(const vtkDebugLeaks&) = delete;
143 void operator=(const vtkDebugLeaks&) = delete;
144};
145
146// This class defines callbacks for debugging tools. The callbacks are not for general use.
147// The objects passed as arguments to the callbacks are in partially constructed or destructed
148// state and accessing them may cause undefined behavior.
149class VTKCOMMONCORE_EXPORT vtkDebugLeaksObserver
150{
151public:
152 virtual ~vtkDebugLeaksObserver() = default;
155};
156
157VTK_ABI_NAMESPACE_END
158#endif // vtkDebugLeaks_h
Manages the vtkDebugLeaks singleton.
virtual ~vtkDebugLeaksObserver()=default
virtual void DestructingObject(vtkObjectBase *)=0
virtual void ConstructingObject(vtkObjectBase *)=0
identify memory leaks at program termination vtkDebugLeaks is used to report memory leaks at the exit...
static void ConstructClass(vtkObjectBase *object)
Call this when creating a class.
static void SetExitError(int)
Get/Set flag for exiting with an error when leaks are present.
static void ConstructingObject(vtkObjectBase *object)
vtkDebugLeaks()=default
static void ClassInitialize()
static void SetDebugLeaksObserver(vtkDebugLeaksObserver *observer)
static std::vector< std::function< void()> > * Finalizers
static void DestructClass(const char *className)
Call this when deleting vtkCommand or a subclass.
static void ConstructClass(const char *className)
Call this when creating a vtkCommand or subclasses.
static int PrintCurrentLeaks()
Print all the values in the table.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
static int DisplayMessageBox(const char *)
static void DestructingObject(vtkObjectBase *object)
static int GetExitError()
Get/Set flag for exiting with an error when leaks are present.
static void AddFinalizer(std::function< void()> finalizer)
Ensure that finalizer is invoked before debug-leaks accounting is reported.
~vtkDebugLeaks() override=default
static vtkDebugLeaks * New()
static vtkDebugLeaksObserver * GetDebugLeaksObserver()
static void ClassFinalize()
static void DestructClass(vtkObjectBase *object)
Call this when deleting a class.
a simple class to control print indentation
Definition vtkIndent.h:108
abstract base class for most VTK objects
friend class vtkDebugLeaks
abstract base class for most VTK objects
Definition vtkObject.h:162