I have a DLL that uses a global CUDA context (to allow all threads to share same GPU memory). I call cuCtxCreate() in a C++ constructor of a global, but it’s not clear where to put cuCtxDestroy() because I have some global variables (image buffers) that call cudaFree in its destructor.
The relative program order is:
- Constructors()
- DLL_PROCESS_ATTACH
- DLL_PROCESS_DETACH
- Destructors()
If I put cuCtxDestroy() in a C++ destructor of a global, there is no guarantee it will be called after the destructors for the image buffers.
The only solution I can think of is to get rid of all global objects that use CUDA in its constructors/destructor and instead dynamically create/destroy those global variables in DLL_PROCESS_ATTACH & DLL_PROCESS_DETACH, but that’s less convenient than being able to declare your global image buffers anywhere in the code.
Is there a better way?