samedi 4 juillet 2020

Latest call in C++

In my C++ library I need to call a C library. The C library has two functions: start() and stop(). In my application I created a singleton like that:

class MySingleton {
private:
    MySingleton() { start(); }
    ~MySingleton() { stop(); }
    //other attributes here, singleton keeps a "state"
public:
    static MySingleton& getInstance() {
        static MySingleton m;
        return m;
    }
};

In other classes dynamically allocated and "tracked" via shared_ptr I call the C library in destructors:

class Foo {
public:
    ~Foo() { //call c library }
};

The problem: I have no control over the destruction of the singleton using a static method. At application exit it could happen that singleton destructor is called before another destructor calling the C library after stop(). What's the best way to avoid that? Ideally I should say "call the destructor of the singleton as latest call", is it possible?

Aucun commentaire:

Enregistrer un commentaire