FAQ 10.14

T

thomas

URL: http://www.parashift.com/c++-faq-lite/ctors.html

The FAQ 10.14 says that "pointer to static object" is preferrable to
"static object" because of problems when deinitialization.

I don't quite get it.

I think static object will exist with the existance of a class. Where
comes the deinitialization?

example: --------code-------------
#include<iostream>
using namespace std;

class Fred{
public:
Fred(){};
void gobowling(){}
};

Fred& x(){
static Fred ans;
return ans;
}

int main(){
x().gobowling();
}
--------------
The static object created with x() will never be destructed until
program exits.

Another example: -----------code----------
#include<iostream>
using namespace std;

class Fred{
public:
Fred(){};
void gobowling(){}
Fred& x(){
static Fred ans;
return ans;
}
};

int main(){
Fred p;
p.x().gobowling();
}
 
J

Joshua Maurice

URL:http://www.parashift.com/c++-faq-lite/ctors.html

The FAQ 10.14 says that "pointer to static object" is preferrable to
"static object" because of problems when deinitialization.

I don't quite get it.

I think static object will exist with the existance of a class. Where
comes the deinitialization?

example:  --------code-------------
#include<iostream>
using namespace std;

class Fred{
public:
        Fred(){};
        void gobowling(){}

};

Fred& x(){
        static Fred ans;
        return ans;

}

int main(){
        x().gobowling();}

--------------
The static object created with x() will never be destructed until
program exits.

Another example:  -----------code----------
#include<iostream>
using namespace std;

class Fred{
public:
        Fred(){};
        void gobowling(){}
        Fred& x(){
                static Fred ans;
                return ans;
        }

};

int main(){
        Fred p;
        p.x().gobowling();}

----------------------
Here I'm not sure whether the static object will be destructed. But x
() method cannot be called if the instance of "p" is destroyed. So I
think the FAQ means this case. Still I'm not quite sure.
Can anyone clarify this?

Try running this program:

#include <iostream>
using namespace std;

class foo
{
public:
foo() { cout << "foo::foo()" << endl; }
~foo() { cout << "foo::~foo()" << endl; }
};

foo x;

int main() {}

And try running this program

#include <iostream>
using namespace std;

class foo
{
public:
foo() { cout << "foo::foo()" << endl; }
~foo() { cout << "foo::~foo()" << endl; }
static foo& getTheFoo()
{ static foo x;
return x;
}
};

int main() { foo::getTheFoo(); }

In both cases, the static object is destroyed, as shown by the prints
to stdout. That's what they're talking about in the FAQ. When your
program is dying, it calls destructors of static objects. If a crash
happens because they're called in a bad order, it might not be the bad
in your case, or it might be bad. Perhaps your destructors do things
required for program correctness, like flush a log file.
 
T

thomas

Try running this program:

  #include <iostream>
  using namespace std;

  class foo
  {
  public:
    foo() { cout << "foo::foo()" << endl; }
    ~foo() { cout << "foo::~foo()" << endl; }
  };

  foo x;

  int main() {}

And try running this program

  #include <iostream>
  using namespace std;

  class foo
  {
  public:
    foo() { cout << "foo::foo()" << endl; }
    ~foo() { cout << "foo::~foo()" << endl; }
    static foo& getTheFoo()
    { static foo x;
      return x;
    }
  };

  int main() { foo::getTheFoo(); }

In both cases, the static object is destroyed, as shown by the prints
to stdout. That's what they're talking about in the FAQ. When your
program is dying, it calls destructors of static objects. If a crash
happens because they're called in a bad order, it might not be the bad
in your case, or it might be bad. Perhaps your destructors do things
required for program correctness, like flush a log file.- Hide quoted text -

- Show quoted text -

en. So the FAQ talks about the bad destruction order for static
objects. Interesting!
But it's still not quite clear how "pointer to static object" avoids
such problem?
Will data allocated in heap be destructed? Or will the memory they
occupies just be reclaimed by the operating system as a last routine
of program executing?
 
J

Joshua Maurice

en. So the FAQ talks about the bad destruction order for static
objects. Interesting!
But it's still not quite clear how "pointer to static object" avoids
such problem?
Will data allocated in heap be destructed? Or will the memory they
occupies just be reclaimed by the operating system as a last routine
of program executing?

Heap objects are not destroyed unless you explicitly destroy them.
Most / all "modern" desktop operating systems reclaim memory of
programs when they die, along with open file handles, etc., but they
know nothing of C++ objects, and they will not call destructors on
them.
 
B

Bo Persson

thomas said:
en. So the FAQ talks about the bad destruction order for static
objects. Interesting!
But it's still not quite clear how "pointer to static object" avoids
such problem?
Will data allocated in heap be destructed? Or will the memory they
occupies just be reclaimed by the operating system as a last routine
of program executing?

The FAQ author "solves" the problem of potentially using a destroyed
object from other destructors, by not destroying it at all. This might
sometimes be a quick fix, unless the destructor of *that* object does
something important.

The real solution is of course to organize your program so that you
don't have lots of static objects, whose destructors refer to lots of
other static objects. :)


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top