I am trying to track down a crash I am seeing when aborting SignedMaurerDistanceMapImageFilter while it is generating a distance map. The crash occurs at line 299 in itkImageSource.hxx, ThreaderCallback() :
ThreadIdType total = str->Filter->SplitRequestedRegion(threadId, threadCount, splitRegion);
The issue is that the Filter smart pointer is pointing to nullptr at this point, therefore causing a crash.
I have tracked this down to the clear up code in the PoolMultiThreader class, SingleMethodExecute() function. The ProcessAborted exception is caught (line 110), and the threads are then waited on to ensure they have all terminated. The issue, I believe is that if any of the remaining threads also throw an exception (likely to be ProcessAborted) during this clear up process, it is re-thrown (line 123) meaning that any remaining threads are not waited for.
In SignedMaurerDistanceMapImageFilter::GenerateData(), the ThreadStruct is allocated on the stack, meaning that when the exception gets to this level, the variable str goes out of scope and therefore its Filter member is cleared up. I believe this leads to the crash in ImageSource as any threads still running will attempt to access the Filter member (and the now out of scope and destructed str variable).
I have commented out the throw on line 123 of itkPoolMultiThreader.cxx and rebuilt ITK to check my theory and this change does seems to solve the problem. Does anyone have any thoughts on this? Is this the best solution to this or is there a better one?
I am using ITK 5.0.1 and the machine I am running on is a 12 core hyperthreaded AMD Ryzen so there will potentially be a lot of thread pool threads running concurrently which may cause the issue to show more easily.
Thanks in advance for any ideas,