I have the following classes in my code. In other words, There is a static object (singletone) which creates thread in CTor, and when its DTor is called, it has some work to be done in the context of this thread (DTor puts some jobs for the thread).
The problem that i face is that when the DTor of B is called there are no other threads running in the process - seems like this thread is killed by process cleanup before calling the destructor of class B.
Anyone knows why this happens? and how to avoid it?
UPD: The problems occures only when Singleton is created from DLL. All works fine when Singleton is created from the same executable.
I am using VS2017
Singleton.dll (A.h + A.cpp)
A.h -->
#pragma once
#include <thread>
class __declspec(dllexport) A
{
public:
static A* instance();
A();
~A();
private:
bool stopFlag;
std::thread mThread;
};
A.cpp
#include "stdafx.h"
#include <thread>
#include "A.h"
using namespace std;
A::A()
{
mThread = std::thread([this] { while (stopFlag == false) { } });
}
A::~A()
{
stopFlag = true;
mThread.join();
}
A* A::instance()
{
static A self;
return &self;
}
================================================================================
Executable which uses DLL
main.cpp
#include "stdafx.h"
#include "A.h"
int main()
{
auto a = A::instance();
return 0;
}
Updated with the compilable code. Now if you compile the first two files as DLL, and then put breakpoint in A's destructor, you will see that thread with lambda function does not exists....
UPDATE: Found an answer by myslef. In Windows, static object from DLL are unloaded ay very last point when all threads are already cleaned up https://msdn.microsoft.com/en-us/library/windows/desktop/dn633971(v=vs.85).aspx
Statics as Local, Persistent Variables Variables that are local to a function, usually have automatic storage, and are created upon entry to the function and destroyed upon exit. However, if a local variable to a function is tagged static, the variable will persist across multiple invocations of the function.
Static variables are the variables which once declared, they get destroyed only when the program has completed its execution. They have a property of retaining their previous scope value if they are already declared once in the program.
don't delete it; in most cases, there's really no reason to call the destructor, and if you happen to use the instance in the destructors of other static objects, you'll run into an order of destruction problem.
Static object destructors This means that it can be dangerous to call exit( ) inside a destructor because you can end up with infinite recursion. Static object destructors are not called if you exit the program using the Standard C library function abort( ).
Found an answer by myslef. In Windows, static object from DLL are unloaded ay very last point when all threads are already cleaned up https://msdn.microsoft.com/en-us/library/windows/desktop/dn633971(v=vs.85).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With