Fix ANSOCR TensorRT Release

This commit is contained in:
2026-03-30 09:24:04 +11:00
parent b735931c55
commit 01eabf76bd
5 changed files with 57 additions and 3 deletions

View File

@@ -8,6 +8,8 @@
#include "ANSGpuFrameRegistry.h"
#include <json.hpp>
#include "NV12PreprocessHelper.h"
#include "engine/TRTEngineCache.h"
#include "engine/EnginePoolManager.h"
#include <unordered_map>
#include <condition_variable>
#include <cstdint>
@@ -93,7 +95,32 @@ BOOL APIENTRY DllMain(HMODULE hModule,
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
// When lpReserved != NULL, the process is terminating via ExitProcess.
// The OS has already killed all worker threads (idle timers, CUDA
// threads, etc.). Set the global flag so atexit destructors skip
// thread joins and CUDA/TRT cleanup that would fail on a dead context.
if (lpReserved != nullptr) {
g_processExiting().store(true, std::memory_order_relaxed);
break;
}
// Dynamic FreeLibrary — threads are still alive, safe to clean up.
try {
std::vector<ANSCENTER::ANSOCRBase*> leakedHandles;
{
std::lock_guard<std::mutex> lk(OCRHandleRegistryMutex());
for (auto& [h, _] : OCRHandleRegistry())
leakedHandles.push_back(h);
OCRHandleRegistry().clear();
}
for (auto* h : leakedHandles) {
try { h->Destroy(); delete h; } catch (...) {}
}
try { EnginePoolManager<float>::instance().clearAll(); } catch (...) {}
try { TRTEngineCache::instance().clearAll(); } catch (...) {}
} catch (...) {}
break;
}
return TRUE;