Are static members destroyed while there are still outstanding cases?

I need to access the static data member from the destructor, but when I exit the program, it seems that it cannot be guaranteed that it still exists! For some reason, static members are destroyed while there are still outstanding class instances. This is strange because I have never heard the advice “Never get static elements from a destructor” before, and yet I think I know about such a restriction, if it exists.

I will give a specific example:

class MyClass {
    public:
        ~MyClass() { m_instances.erase(m_name); }

    private:
        long m_name;
        static std::map<long, MyClass*> m_instances;
};

In another class, I tried the following nasty hack that seemed to work, although when I think about it, I don't think this is really a solution at all.

class MyClass {
    friend class Switch;

    public:
        ~MyClass() { if (m_alive) m_instances.erase(m_name); }

    private:

        static bool m_alive;
        class Switch {
            ~Switch() { MyClass::m_alive = false; }
        };
        static Switch m_switch;

        long m_name;
        static std::map<long, MyClass*> m_instances;
};

, MyClass m_instances, m_switch? m_switch , m_alive "" , , "true" ( , ).

- ? , - .

+3
3

. - :

class MyClass {
    public:
        MyClass() { add_instance(m_name, this); };
        ~MyClass() { erase_instance(m_name); }

    private:
        long m_name;
        static std::map<long, MyClass*>& get_instance_map();
        static void add_instance(long aName, MyClass* aObj);
        static void erase_instance(long aName);
};

std::map<long, MyClass*>* MyClass::get_instance_map() {
  static std::map<long, MyClass*>* p_inst = new std::map<long, MyClass*>();
  return p_inst;
};

void MyClass::add_instance(long aName, MyClass* aObj) {
  static std::map<long, MyClass*>* p_inst = MyClass::get_instance_map();
  p_inst->insert( std::make_pair(aName, aObj) );
};

void MyClass::erase_instance(long aName) {
  static std::map<long, MyClass*>* p_inst = MyClass::get_instance_map();
  p_inst->erase( aName );
};

, , . . , std:: map, , , , , freestore, "" , , .

+2

, . ++ (shared_ptr ).

, . , , " " (: ).

, myClass , :

# 1:

#include <iostream>

struct A {
    A() { std::cout << "*A "; };
   ~A() { std::cout << "~A "; };
};

struct B {
    B() { std::cout << "*B "; };
   ~B() { std::cout << "~B "; };

    static A a;
};

B t;
A B::a;

int main() {}

// Output: *B *A ~A ~B 

, B B.

# 2:

#include <iostream>

struct A {
    A() { std::cout << "*A "; };
   ~A() { std::cout << "~A "; };
};

struct B {
    B() { std::cout << "*B "; };
   ~B() { std::cout << "~B "; };

    static A a;
};

A B::a;
B t;

int main() {}

// Output: *A *B ~B ~A 

. , - ; , ... , !

+2

, , MyClass , . , , .

It is also possible that you have another global (static) memory corruption. If this is the case, it may mean that rewriting one global one may overwrite other objects next to global variables located in the same memory space, that is, the static problems you encountered were damaged and not deleted.

0
source

All Articles