I am writing a console C++ program on Visual Studio 2019, where I am using a third party camera lib. This lib needs me to call libInitialize() on start and libTerminate() at the end of my program. If I do not call libTerminate(), the next time I start the program I get an error saying that the camera is still being used by another program. Everything is fine as long as I properly exit the program. But if I terminate the program by closing the console window or pressing the stop button in the debugger the call to libTerminate() is never made. So my question is, is there any way to have code executed whenever the program is terminated unexpectedly?
Asked
Active
Viewed 133 times
7
-
4Maybe ```std::at_quick_exit``` is what you need! please take a look at [at_quick_exit](https://en.cppreference.com/w/cpp/utility/program/at_quick_exit). Also [atexit](https://en.cppreference.com/w/cpp/utility/program/atexit) – AKL May 20 '19 at 11:34
-
1Why not call libTerminate before calling libInitialize whenever you start the program? – Wander3r May 20 '19 at 11:37
-
As you are on Windows you might like to read https://blogs.msdn.microsoft.com/larryosterman/2004/09/10/structured-exception-handling-considered-harmful/ – Richard Critten May 20 '19 at 11:38
-
2You should investigate the reason why camera is considered to be used by another program. There is absolutely no way to execute anything if program is terminated unexpectedly. – user7860670 May 20 '19 at 11:44
-
1I would say library/driver is faulty. When program is terminated system should be able to release all related resources. This is OS responsibility. – Marek R May 20 '19 at 11:45
-
2@AKL that's completely worthless, `std::quick_exit` does not occur upon unexpected program termination. – user7860670 May 20 '19 at 11:46
-
If there is a way to release the camera (handle?) from another process, then you could have a helper launcher/monitor process watching over your main process. – Igor G May 20 '19 at 12:13
-
You could create a DLL and put `libInitialize` and `libTerminate` in `DllMain`. I think Windows will unload the DLL properly even if the program exits unexpectedly. – Ted Lyngmo May 20 '19 at 12:55
-
You could create a lock file on startup, and remove it on exit. If the lock file is there, when you start (I.e. if the program exits unexpectedly), then you call libTerminate() before calling libInitialize(). – R0m1 May 20 '19 at 13:20
-
@TedLyngmo - if process will be terminate externally - of course any code in process will be not called. so this is only partial solution. however dll here not need . possible set tls callback in exe. it also called with `DLL_PROCESS_DETACH` – RbMm May 20 '19 at 13:37
-
@RbMm Ok, wasn't sure. I was hoping that the call to `DlllMain` would come directly from the OS somehow. – Ted Lyngmo May 20 '19 at 14:00
-
@TedLyngmo - no. this is false. `DLL_PROCESS_DETACH` called from ExitProcess - > LdrShutdownProcess. if kill process - of course this will be not called – RbMm May 20 '19 at 14:07
-
@RbMm You are 100% correct. Just verified it. – Ted Lyngmo May 20 '19 at 14:19
-
solved it through signal handling, especially handling SIGINT and SIGSEGV. – Jojo71 May 21 '19 at 12:06