diff --git a/projects/clr/rocclr/device/pal/paldevice.hpp b/projects/clr/rocclr/device/pal/paldevice.hpp index 25ad183166..c683f4d232 100644 --- a/projects/clr/rocclr/device/pal/paldevice.hpp +++ b/projects/clr/rocclr/device/pal/paldevice.hpp @@ -27,6 +27,7 @@ #include "platform/perfctr.hpp" #include "platform/threadtrace.hpp" #include "platform/memory.hpp" +#include "platform/runtime.hpp" #include "utils/concurrent.hpp" #include "thread/thread.hpp" #include "thread/monitor.hpp" @@ -405,8 +406,12 @@ class Device : public NullDevice { //! Free resource cache on device if OCL context was destroyed. //! @note: Backend device doesn't track resources per context and releases all resources, regardless //! the number of still active contexts - virtual void ContextDestroy() { resourceCache().free(); } - + virtual void ContextDestroy() { + // The if condition is a best effort to avoid crash if the function is called after DLL detached + if (!amd::Runtime::isLibraryDetached()) { + resourceCache().free(); + } + } //! Validates kernel before execution virtual bool validateKernel(const amd::Kernel& kernel, //!< AMD kernel object const device::VirtualDevice* vdev, bool coop_group = false); diff --git a/projects/clr/rocclr/platform/runtime.cpp b/projects/clr/rocclr/platform/runtime.cpp index eff552d54a..2096399944 100644 --- a/projects/clr/rocclr/platform/runtime.cpp +++ b/projects/clr/rocclr/platform/runtime.cpp @@ -46,6 +46,7 @@ namespace amd { volatile bool Runtime::initialized_ = false; +bool Runtime::LibraryDetached = false; bool Runtime::init() { if (initialized_) { diff --git a/projects/clr/rocclr/platform/runtime.hpp b/projects/clr/rocclr/platform/runtime.hpp index 45e15523b6..f582e70323 100644 --- a/projects/clr/rocclr/platform/runtime.hpp +++ b/projects/clr/rocclr/platform/runtime.hpp @@ -32,6 +32,7 @@ namespace amd { class Runtime : AllStatic { static volatile bool initialized_; + static bool LibraryDetached; public: //! Return true if the OpencCL runtime is already initialized @@ -45,6 +46,14 @@ class Runtime : AllStatic { //! Return true if the Runtime is still single-threaded. static bool singleThreaded() { return !initialized(); } + + //! Return whether the library is detached by OS + static bool isLibraryDetached() { return LibraryDetached; } + + //! Set the library has been detached. + static void setLibraryDetached() { + LibraryDetached = true; + } }; #if 0