C++ static class members destruction order. 
Author Message
 C++ static class members destruction order.

Here is four code files, belongs to one VC++ project:

1. B.h:
    ...
    class A {
        A();
        ~A();

        DoingSomething();
    ...
    };

    class B {
        B();
        ~B();
    ...
    static A m_A;
    };

2. B.cpp:
    #include "B.h"

    A B::m_A();
    ...
    B::~B() {
        m_A.DoingSomething();
    }
    ...

3. C.h
    #include "B.h"
    ...
    class C {
    ...
    static B m_B;
    };

4. C.cpp
    #include "C.h"

    B C::m_B();
    ...

    The question is, can I be shure, that this code will work correctly -
that is, will the C::m_B destructor called _always_ before B::m_A()
destructor, or it's unpredictable?
                Regards,
                        Aleksey Osipenko.



Sun, 25 May 2003 05:02:08 GMT  
 C++ static class members destruction order.

Quote:

> Here is four code files, belongs to one VC++ project:

> 1. B.h:
>     ...
>     class A {
>         A();
>         ~A();

>         DoingSomething();
>     ...
>     };

>     class B {
>         B();
>         ~B();
>     ...
>     static A m_A;
>     };

> 2. B.cpp:
>     #include "B.h"

>     A B::m_A();
>     ...
>     B::~B() {
>         m_A.DoingSomething();
>     }
>     ...

> 3. C.h
>     #include "B.h"
>     ...
>     class C {
>     ...
>     static B m_B;
>     };

> 4. C.cpp
>     #include "C.h"

>     B C::m_B();
>     ...

>     The question is, can I be shure, that this code will work correctly -
> that is, will the C::m_B destructor called _always_ before B::m_A()
> destructor, or it's unpredictable?

You can't count on this.

Sergei



Sun, 25 May 2003 05:33:01 GMT  
 C++ static class members destruction order.
The C++ standard does not specific static variable destruction orders
between *different* modules.  I'm not what happens if they are in the same
file though.  I believe there is a design pattern that addresses this issue.
I'll look it up when I get back to Canada (on a business trip right now).

- Joel



Quote:
> > Here is four code files, belongs to one VC++ project:

> > 1. B.h:
> >     ...
> >     class A {
> >         A();
> >         ~A();

> >         DoingSomething();
> >     ...
> >     };

> >     class B {
> >         B();
> >         ~B();
> >     ...
> >     static A m_A;
> >     };

> > 2. B.cpp:
> >     #include "B.h"

> >     A B::m_A();
> >     ...
> >     B::~B() {
> >         m_A.DoingSomething();
> >     }
> >     ...

> > 3. C.h
> >     #include "B.h"
> >     ...
> >     class C {
> >     ...
> >     static B m_B;
> >     };

> > 4. C.cpp
> >     #include "C.h"

> >     B C::m_B();
> >     ...

> >     The question is, can I be shure, that this code will work
correctly -
> > that is, will the C::m_B destructor called _always_ before B::m_A()
> > destructor, or it's unpredictable?

> You can't count on this.

> Sergei



Sun, 25 May 2003 03:00:00 GMT  
 C++ static class members destruction order.

Quote:

> The C++ standard does not specific static variable destruction
> orders between *different* modules.  I'm not what happens if they
> are in the same file though.  I believe there is a design pattern
> that addresses this issue.  I'll look it up when I get back to
> Canada (on a business trip right now).

Inside the same file, they are created in the order they're declared,
and destroyed in the opposite order.

Considering each file as a group of objects, you do not know the
relative group orders, just the orders inside each group.  The linker
can initialize one file first in one build, then rearrange it later
for some reason on a later build.  Any dependencies upon construction
order between files is a flawed design.

(The singleton pattern can help solve this problem by allocating the
object on an as-needed basis, thereby guaranteeing the object always
exists when it is used, and wasting no memory for objects that aren't.)

--
Chris



Sun, 25 May 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. initialization order of static class member

2. Order of construction of static class members ?

3. Static member in C++ class

4. accessing static class member in a dll in c++

5. who makes the destruction of static classes?

6. Order of static member func execution before main()

7. does static class member change class size?

8. Class object as static member of another class

9. how to call a non-static member function of a class from a static member function of the class?

10. accessing static member of Windows service class thro reflection - need help

11. Initialising a static data member of a Class?

12. Initialising a static data member of a Class?

 

 
Powered by phpBB® Forum Software