Skip to content

Commit

Permalink
Add Schwartz counters for consistent static destruction.
Browse files Browse the repository at this point in the history
Static builds of paraview were not freeing
vtkObjectFactory::RegisteredFactories or vtkOutputWindow::Instance. See:

https://open.cdash.org/viewTest.php?onlyfailed&buildid=4572721

This fixes the issue to ensure that they are always freed at process exit.
  • Loading branch information
David C. Lonie committed Oct 3, 2016
1 parent 874a7d8 commit cd06ce0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
21 changes: 9 additions & 12 deletions Common/Core/vtkObjectFactory.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,20 @@


vtkObjectFactoryCollection* vtkObjectFactory::RegisteredFactories = 0;
static unsigned int vtkObjectFactoryRegistryCleanupCounter = 0;

vtkObjectFactoryRegistryCleanup::vtkObjectFactoryRegistryCleanup()
{
++vtkObjectFactoryRegistryCleanupCounter;
}

class vtkCleanUpObjectFactory
vtkObjectFactoryRegistryCleanup::~vtkObjectFactoryRegistryCleanup()
{
public:
inline void Use()
{
}
~vtkCleanUpObjectFactory()
if (--vtkObjectFactoryRegistryCleanupCounter == 0)
{
vtkObjectFactory::UnRegisterAllFactories();
vtkObjectFactory::UnRegisterAllFactories();
}
};

static vtkCleanUpObjectFactory vtkCleanUpObjectFactoryGlobal;
}

// Create an instance of a named vtk object using the loaded
// factories
Expand Down Expand Up @@ -88,7 +87,6 @@ void vtkObjectFactory::ConstructInstance(const char *vtkclassname)
// A one time initialization method.
void vtkObjectFactory::Init()
{
vtkCleanUpObjectFactoryGlobal.Use();
// Don't do anything if we are already initialized
if(vtkObjectFactory::RegisteredFactories)
{
Expand Down Expand Up @@ -737,4 +735,3 @@ void vtkObjectFactory::CreateAllInstance(const char* vtkclassname,
}
}
}

15 changes: 15 additions & 0 deletions Common/Core/vtkObjectFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#ifndef vtkObjectFactory_h
#define vtkObjectFactory_h

#include "vtkDebugLeaksManager.h" // Must be included before singletons
#include "vtkCommonCoreModule.h" // For export macro
#include "vtkObject.h"

Expand Down Expand Up @@ -286,6 +287,20 @@ class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
void operator=(const vtkObjectFactory&) VTK_DELETE_FUNCTION;
};

// Implementation detail for Schwartz counter idiom.
class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
{
public:
vtkObjectFactoryRegistryCleanup();
~vtkObjectFactoryRegistryCleanup();

private:
vtkObjectFactoryRegistryCleanup(const vtkObjectFactoryRegistryCleanup& other) VTK_DELETE_FUNCTION;
vtkObjectFactoryRegistryCleanup& operator=(const vtkObjectFactoryRegistryCleanup& rhs) VTK_DELETE_FUNCTION;
};
static vtkObjectFactoryRegistryCleanup vtkObjectFactoryRegistryCleanupInstance;


// Macro to create an object creation function.
// The name of the function will by vtkObjectFactoryCreateclassname
// where classname is the name of the class being created
Expand Down
13 changes: 7 additions & 6 deletions Common/Core/vtkOutputWindow.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
#endif
#include "vtkCommand.h"
#include "vtkObjectFactory.h"
#include "vtkDebugLeaks.h"


//----------------------------------------------------------------------------

vtkOutputWindow* vtkOutputWindow::Instance = 0;
vtkOutputWindowCleanup vtkOutputWindow::Cleanup;
static unsigned int vtkOutputWindowCleanupCounter = 0;

void vtkOutputWindowDisplayText(const char* message)
{
Expand Down Expand Up @@ -57,12 +54,16 @@ void vtkOutputWindowDisplayDebugText(const char* message)

vtkOutputWindowCleanup::vtkOutputWindowCleanup()
{
++vtkOutputWindowCleanupCounter;
}

vtkOutputWindowCleanup::~vtkOutputWindowCleanup()
{
// Destroy any remaining output window.
vtkOutputWindow::SetInstance(0);
if (--vtkOutputWindowCleanupCounter == 0)
{
// Destroy any remaining output window.
vtkOutputWindow::SetInstance(0);
}
}

vtkOutputWindow::vtkOutputWindow()
Expand Down
10 changes: 5 additions & 5 deletions Common/Core/vtkOutputWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#ifndef vtkOutputWindow_h
#define vtkOutputWindow_h

#include "vtkDebugLeaksManager.h" // Must be included before singletons
#include "vtkCommonCoreModule.h" // For export macro
#include "vtkObject.h"

Expand Down Expand Up @@ -89,11 +90,6 @@ class VTKCOMMONCORE_EXPORT vtkOutputWindow : public vtkObject
vtkSetMacro(PromptUser, int);
//@}

// use this as a way of memory management when the
// program exits the SmartPointer will be deleted which
// will delete the Instance singleton
static vtkOutputWindowCleanup Cleanup;

protected:
vtkOutputWindow();
~vtkOutputWindow() VTK_OVERRIDE;
Expand All @@ -105,4 +101,8 @@ class VTKCOMMONCORE_EXPORT vtkOutputWindow : public vtkObject
void operator=(const vtkOutputWindow&) VTK_DELETE_FUNCTION;
};

// Uses schwartz counter idiom for singleton management
static vtkOutputWindowCleanup vtkOutputWindowCleanupInstance;


#endif

0 comments on commit cd06ce0

Please sign in to comment.