Re: Assign Reference to another Referance
On Sep 26, 1:28 am, Paavo Helde <pa...@nospam.please.ee> wrote:
cpisz <cp...@austin.rr.com> kirjutas:
I'll give you a compilable example as
soon as I get VS fixed later today.
Good! Please do so!
class Singleton
{
public:
static Singleton & Instance()
{
if( !m_instance )
{
m_instance = new Singleton();
}
return *m_instance;
}
static void DoStuff()
{
int x = 1 + 1;
}
private:
static Singleton * m_instance;
};
Singleton * Singleton::m_instance = 0;
class Foo
{
public:
Foo(){}
~Foo()
{
Singleton::Instance().DoStuff();
}
};
int main()
{
static Foo foo;
return 0; // Undefined behavior after this line, when program
cleanup occurs!
}
Please use the code earlier in this post where foo is global.
I thought you claimed that the alleged UB manifests itself by a segfault
in Linux. I would like to see such an example. (This code appeared to
cause no segfault in my quick test.)
Nay, I claimed that it is undefined behavior and as such may or may
not cause a seg fault in linux depending on the alignment of the stars
in the universe. It all depends on which order the memory in which the
foo and m_instance reside, becomes invalid. It may run without a
problem at all, it may not. It could run and be recompiled with one
line of unrelated code added and stop working. It could run on one PC
and not another. It is _undefined_ behavior.
So, it appears the lifetime of the static pointer ends only when the
memory is unmapped from the process space by the OS (storage released).
You are just repeating my own claim back to me.
Except I am not claiming in the specific word you used "unmapped from
process space", but rather simply that, no matter how you'd like to
label it, the OS will make the memory where the static pointer resides
invalid.
I've stopped the execution of it in GCC, I really wish I had written
down the call stack to show you what the heck I am talking about. It
had a special name that was obviously "process cleanup".
I argue that the pointer cannot be considered "destroyed" before its
lifetime ends,
I agree
so it is not "destroyed" while any of the program code still runs,
I agree, depending on what you call "program code".
I am claiming that it is not destroyed before execution is returned
from whatever function you have set to be the entry point of your
application. I have said over and over the probem occurs _after_ your
program exits.
Because, just like the static pointer, a dependant global or static
does not have its destructor called until _after_ your program exits.
If said destructor makes use of the static pointer, the behavior is
undefined!
Are you claiming that the destructor of a global object will never get
called after main returns? Because I know we all agree that is poopy.
I really don't know why everyone is having a hard time wrapping thier
heads around this.
If you claim otherwise, please back up your statements by relevant quotes
from the standard!
It is undefined behavior, as in not defined. There is nothing to
quote.