Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread created by static object deleted before DTor?

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

like image 806
Boris Bolshem Avatar asked Mar 30 '17 07:03

Boris Bolshem


People also ask

Can static variables be destroyed?

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.

Why static variables are not destroyed when function return?

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.

Do we need to delete static pointer?

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.

Can a destructor be static?

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( ).


1 Answers

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

like image 73
Boris Bolshem Avatar answered Sep 24 '22 06:09

Boris Bolshem