From 9835be3f27d94ee58071d17be3c4ddd6411d52cf Mon Sep 17 00:00:00 2001
From: Sylvester Joosten <sjoosten@anl.gov>
Date: Thu, 11 Nov 2021 01:54:24 +0000
Subject: [PATCH] Add Whit's patch to fix dd4hep particle refcount underflow
 issue for now

---
 spack/packages/dd4hep/package.py               |  3 +++
 spack/packages/dd4hep/refcount_underflow.patch | 14 ++++++++++++++
 2 files changed, 17 insertions(+)
 create mode 100644 spack/packages/dd4hep/refcount_underflow.patch

diff --git a/spack/packages/dd4hep/package.py b/spack/packages/dd4hep/package.py
index 6e322c928..b1bfde0f5 100644
--- a/spack/packages/dd4hep/package.py
+++ b/spack/packages/dd4hep/package.py
@@ -51,6 +51,9 @@ class Dd4hep(CMakePackage):
     # See https://github.com/spack/spack/issues/24232
     patch('cmake_language.patch', when='@:1.16.1')
 
+    # hack to fix refcount underflow
+    patch('refcount_underflow.patch', when='@1.17:')
+    
     # custom hash for the 2021-07-27 version, needed to include
     # https://github.com/AIDASoft/DD4hep/pull/849
     # https://github.com/AIDASoft/DD4hep/pull/851
diff --git a/spack/packages/dd4hep/refcount_underflow.patch b/spack/packages/dd4hep/refcount_underflow.patch
new file mode 100644
index 000000000..37b1e12a8
--- /dev/null
+++ b/spack/packages/dd4hep/refcount_underflow.patch
@@ -0,0 +1,14 @@
+diff --git a/DDG4/src/Geant4Particle.cpp b/DDG4/src/Geant4Particle.cpp
+index 1db6105c..7f8d7482 100644
+--- a/DDG4/src/Geant4Particle.cpp
++++ b/DDG4/src/Geant4Particle.cpp
+@@ -57,7 +57,9 @@ Geant4Particle::~Geant4Particle()  {
+ void Geant4Particle::release()  {
+   //::printf("************ Release Geant4Particle[%p]: ID:%d pdgID %d ref:%d\n",(void*)this,id,pdgID,ref-1);
+   if ( --ref <= 0 )  {
++    if( (ref > -1) ) { 
+     delete this;
++    }
+   }
+ }
+ 
-- 
GitLab