Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitk::ReferenceCountWatcher Class Reference

Keeps track of the reference count of an object even if it is destroyed. More...

#include <mitkReferenceCountWatcher.h>

Inheritance diagram for mitk::ReferenceCountWatcher:
Collaboration diagram for mitk::ReferenceCountWatcher:

Public Types

typedef itk::SimpleMemberCommand< ReferenceCountWatcher > CommandType
 

Public Member Functions

 mitkClassMacroItkParent (ReferenceCountWatcher, itk::Object)
 
 ReferenceCountWatcher (itk::Object *o, const char *comment="")
 Constructor requiring the object to be watched and allowing an optional comment. More...
 
 ~ReferenceCountWatcher () override
 Destructor. Removes the observer from the watched object if it still exists. More...
 
int GetReferenceCount () const override
 Returns the reference count of the watched object. More...
 
virtual const char * GetComment () const
 Returns the optional string comment. More...
 

Protected Member Functions

void DeleteObserver ()
 Callback invoked on itk::DeleteEvent() of the watched object. More...
 

Protected Attributes

itk::Object * m_Object
 The object to be watched. More...
 
std::string m_Comment
 Optional comment, e.g. for debugging output. More...
 
bool m_Deleted
 If true, m_Object is no longer valid and the returned reference count will be 0. More...
 
CommandType::Pointer m_DeleteCommand
 itk::Command to get a notification when the object is deleted. More...
 
unsigned long m_ObserverTag
 ITK observer tag for the delete event. More...
 

Detailed Description

Keeps track of the reference count of an object even if it is destroyed.

This utility class registers itself as an ITK observer for the DeleteEvent of the watched object. After the object is destroyed, GetReferenceCount() returns 0 instead of accessing invalid memory.

Definition at line 29 of file mitkReferenceCountWatcher.h.

Member Typedef Documentation

◆ CommandType

Definition at line 32 of file mitkReferenceCountWatcher.h.

Constructor & Destructor Documentation

◆ ReferenceCountWatcher()

mitk::ReferenceCountWatcher::ReferenceCountWatcher ( itk::Object *  o,
const char *  comment = "" 
)
inline

Constructor requiring the object to be watched and allowing an optional comment.

Parameters
othe ITK object whose reference count should be tracked.
commentan optional descriptive string for debugging.

Definition at line 55 of file mitkReferenceCountWatcher.h.

References DeleteObserver(), m_DeleteCommand, m_Object, and m_ObserverTag.

◆ ~ReferenceCountWatcher()

mitk::ReferenceCountWatcher::~ReferenceCountWatcher ( )
inlineoverride

Destructor. Removes the observer from the watched object if it still exists.

Definition at line 66 of file mitkReferenceCountWatcher.h.

References m_Deleted, m_Object, and m_ObserverTag.

Member Function Documentation

◆ DeleteObserver()

void mitk::ReferenceCountWatcher::DeleteObserver ( )
inlineprotected

Callback invoked on itk::DeleteEvent() of the watched object.

Definition at line 93 of file mitkReferenceCountWatcher.h.

References m_Deleted.

Referenced by ReferenceCountWatcher().

◆ GetComment()

virtual const char* mitk::ReferenceCountWatcher::GetComment ( ) const
virtual

Returns the optional string comment.

◆ GetReferenceCount()

int mitk::ReferenceCountWatcher::GetReferenceCount ( ) const
inlineoverride

Returns the reference count of the watched object.

Returns
The reference count, 0 if the object has been destroyed, or -1 if no object was provided.

Definition at line 79 of file mitkReferenceCountWatcher.h.

References m_Deleted, and m_Object.

◆ mitkClassMacroItkParent()

mitk::ReferenceCountWatcher::mitkClassMacroItkParent ( ReferenceCountWatcher  ,
itk::Object   
)

Member Data Documentation

◆ m_Comment

std::string mitk::ReferenceCountWatcher::m_Comment
protected

Optional comment, e.g. for debugging output.

Definition at line 41 of file mitkReferenceCountWatcher.h.

◆ m_DeleteCommand

CommandType::Pointer mitk::ReferenceCountWatcher::m_DeleteCommand
protected

itk::Command to get a notification when the object is deleted.

Definition at line 47 of file mitkReferenceCountWatcher.h.

Referenced by ReferenceCountWatcher().

◆ m_Deleted

bool mitk::ReferenceCountWatcher::m_Deleted
protected

If true, m_Object is no longer valid and the returned reference count will be 0.

Definition at line 44 of file mitkReferenceCountWatcher.h.

Referenced by DeleteObserver(), GetReferenceCount(), and ~ReferenceCountWatcher().

◆ m_Object

itk::Object* mitk::ReferenceCountWatcher::m_Object
protected

The object to be watched.

Definition at line 38 of file mitkReferenceCountWatcher.h.

Referenced by GetReferenceCount(), ReferenceCountWatcher(), and ~ReferenceCountWatcher().

◆ m_ObserverTag

unsigned long mitk::ReferenceCountWatcher::m_ObserverTag
protected

ITK observer tag for the delete event.

Definition at line 94 of file mitkReferenceCountWatcher.h.

Referenced by ReferenceCountWatcher(), and ~ReferenceCountWatcher().


The documentation for this class was generated from the following file: