VTK  9.7.1
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 
53 VTK_ABI_NAMESPACE_BEGIN
54 class vtkDebugLeaksHashTable;
55 class vtkDebugLeaksTraceManager;
57 
58 class VTKCOMMONCORE_EXPORT vtkDebugLeaks : public vtkObject
59 {
60 public:
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 
100  static void SetDebugLeaksObserver(vtkDebugLeaksObserver* observer);
101  static vtkDebugLeaksObserver* GetDebugLeaksObserver();
102 
116  static void AddFinalizer(std::function<void()> finalizer);
117 
118 protected:
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 
132  friend class vtkDebugLeaksManager;
133  friend class vtkObjectBase;
134 
135 private:
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.
149 class VTKCOMMONCORE_EXPORT vtkDebugLeaksObserver
150 {
151 public:
152  virtual ~vtkDebugLeaksObserver() = default;
153  virtual void ConstructingObject(vtkObjectBase*) = 0;
154  virtual void DestructingObject(vtkObjectBase*) = 0;
155 };
156 
157 VTK_ABI_NAMESPACE_END
158 #endif // vtkDebugLeaks_h
abstract base class for most VTK objects
Definition: vtkObject.h:52
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
a simple class to control print indentation
Definition: vtkIndent.h:28
friend class vtkDebugLeaks
abstract base class for most VTK objects
Definition: vtkObjectBase.h:68
identify memory leaks at program termination vtkDebugLeaks is used to report memory leaks at the exit...
Definition: vtkDebugLeaks.h:58
static std::vector< std::function< void()> > * Finalizers
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
void operator=(const vtkObjectBase &)
Manages the vtkDebugLeaks singleton.