Skip to content

Commit

Permalink
BUG: Fix OutputWindow thread-safety GetInstance
Browse files Browse the repository at this point in the history
Address segmentation fault uncounted with SimpleITK when the first
call to SimpleITK/ITK are concurrently in threads. This follow the
existing pattern in ITK which has mutex locks around initializing
global instances.
  • Loading branch information
blowekamp authored and dzenanz committed Sep 19, 2022
1 parent 925ddc5 commit 7a121ac
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Modules/Core/Common/src/itkOutputWindow.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
#include "itkOutputWindow.h"
#include "itkObjectFactory.h"
#include "itkSingleton.h"
#include <mutex>

namespace itk
{

struct OutputWindowGlobals
{
OutputWindow::Pointer m_Instance{ nullptr };
std::recursive_mutex m_StaticInstanceLock;
};

/**
Expand Down Expand Up @@ -115,6 +117,7 @@ OutputWindow::Pointer
OutputWindow::GetInstance()
{
itkInitGlobalsMacro(PimplGlobals);
std::lock_guard<std::recursive_mutex> mutexHolder(m_PimplGlobals->m_StaticInstanceLock);
if (!m_PimplGlobals->m_Instance)
{
// Try the factory first
Expand All @@ -141,6 +144,8 @@ void
OutputWindow::SetInstance(OutputWindow * instance)
{
itkInitGlobalsMacro(PimplGlobals);

std::lock_guard<std::recursive_mutex> mutexHolder(m_PimplGlobals->m_StaticInstanceLock);
if (m_PimplGlobals->m_Instance == instance)
{
return;
Expand Down

0 comments on commit 7a121ac

Please sign in to comment.