diff --git a/src/CHIPBackend.cc b/src/CHIPBackend.cc index b7b16a70d..aec94746d 100644 --- a/src/CHIPBackend.cc +++ b/src/CHIPBackend.cc @@ -430,11 +430,16 @@ void chipstar::Module::invalidateDeviceVariablesNoLock() { void chipstar::Module::deallocateDeviceVariablesNoLock( chipstar::Device *Device) { + if (!DeviceVariablesAllocated_) + return; invalidateDeviceVariablesNoLock(); for (auto *Var : ChipVars_) { - auto Err = Device->getContext()->free(Var->getDevAddr()); - (void)Err; + if (!::Backend->getReinitializeFlag()) { + auto Err = Device->getContext()->free(Var->getDevAddr()); + (void)Err; + } Var->setDevAddr(nullptr); + Var->setChipContext(nullptr); } DeviceVariablesAllocated_ = false; } diff --git a/src/CHIPBackend.hh b/src/CHIPBackend.hh index efcb0b989..4ee4eaea9 100644 --- a/src/CHIPBackend.hh +++ b/src/CHIPBackend.hh @@ -1825,6 +1825,7 @@ public: */ class Backend { protected: + bool ReinitializeFlag_ = false; chipstar::EventMonitor *EventMonitor_ = nullptr; int MinQueuePriority_; @@ -1838,6 +1839,8 @@ protected: std::shared_ptr Logger; public: + void setReinitializeFlag(bool Flag) { ReinitializeFlag_ = Flag; } + bool getReinitializeFlag() const { return ReinitializeFlag_; } chipstar::Context *PrimaryContext = nullptr; std::stack ChipCtxStack; diff --git a/src/CHIPDriver.cc b/src/CHIPDriver.cc index 64c9c45c4..5430d413d 100644 --- a/src/CHIPDriver.cc +++ b/src/CHIPDriver.cc @@ -185,6 +185,7 @@ extern hipError_t CHIPReinitialize(const uintptr_t *NativeHandles, Backend->initializeFromNative(NativeHandles, NumHandles); Backend->getActiveDevice()->addFromModuleState(ModuleState); + Backend->setReinitializeFlag(true); return hipSuccess; }