Topic: Visual C++ and destruction of global objects and static members.


Author: "John D." <mikishorNOSPAM@hotmail.com>
Date: Fri, 22 Jun 2001 17:54:25 GMT
Raw View
Your test is not accurate. Place a breakpoint on the destructor and you'll
see that it gets called 3 times, it's just that cout fails and it displays
nothing.


"Antonio Carlos Dom   nguez Brito" <acdbrito@serdis.dis.ulpgc.es> wrote in
message news:3B31F005.FB0D799D@serdis.dis.ulpgc.es...
Well, I hope anybody out there can bring a bit of light here. I think I
can explain better my problem with an minimum example of code, have a
look at the following code:

*** CODE BEGINNING ***

#include <iostream>

using namespace std;

class A
{
    public:
        A(char* pName):_pName_(pName) { cout<<"A["<<_pName_<<"]::A()\n";
}
        ~A() {  cout<<"A["<<_pName_<<"]::~A()\n"; }
    private:
        char* _pName_;
        static A _staticA_;
};

A A::_staticA_("_staticA_");

A a("a");

int main(int argc, char* argv[])
{
    A b("b");
    return 0;
}

*** CODE END ***

and here is its output once I've compiled it using Visual C++ 6.0

*** OUTPUT BEGINNING ***

A[_staticA_]::A()
A[a]::A()
A[b]::A()
A[b]::~A()

*** OUTPUT END ***

as you can see here, the destructor of the global object a, and the
destructor of the static member object of A, _staticA_, are not invoked
when the application finishes. A priori, I thought it was just a
question of configuring the compiler, but really before looking around
on the compiler help, and on the web, I couldn   t find anything to
"configure" the compiler conveniently. I don   t think is a problem of the
compiler, because I think it would a violation of C++ standard, so I
suppose I didn   t look at the correct places. Does anybody knows anything
more conclusive about the topic? The thing is that I   m using a static
member object to intialize and finalize a library, using the constructor
and the destructor of a class, and with this problem, the finalization
is never invoked, and the library is ill-finished.

Thank you very much in advance.

PD: I   m using Visual C++ 6.0 and Windows 2000
--
Antonio Carlos Dom   nguez Brito (acdbrito@dis.ulpgc.es)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Antonio Carlos =?iso-8859-1?Q?Dom=EDnguez?= Brito
Date: Fri, 22 Jun 2001 07:03:06 GMT
Raw View
Well, I hope anybody out there can bring a bit of light here. I think I
can explain better my problem with an minimum example of code, have a
look at the following code:

*** CODE BEGINNING ***

#include <iostream>

using namespace std;

class A
{
    public:
        A(char* pName):_pName_(pName) { cout<<"A["<<_pName_<<"]::A()\n";
}
        ~A() {  cout<<"A["<<_pName_<<"]::~A()\n"; }
    private:
        char* _pName_;
        static A _staticA_;
};

A A::_staticA_("_staticA_");

A a("a");

int main(int argc, char* argv[])
{
    A b("b");
    return 0;                                  =20
}

*** CODE END ***

and here is its output once I've compiled it using Visual C++ 6.0

*** OUTPUT BEGINNING ***

A[_staticA_]::A()
A[a]::A()
A[b]::A()
A[b]::~A()

*** OUTPUT END ***

as you can see here, the destructor of the global object a, and the
destructor of the static member object of A, _staticA_, are not invoked
when the application finishes. A priori, I thought it was just a
question of configuring the compiler, but really before looking around
on the compiler help, and on the web, I couldn=B4t find anything to
"configure" the compiler conveniently. I don=B4t think is a problem of th=
e
compiler, because I think it would a violation of C++ standard, so I
suppose I didn=B4t look at the correct places. Does anybody knows anythin=
g
more conclusive about the topic? The thing is that I=B4m using a static
member object to intialize and finalize a library, using the constructor
and the destructor of a class, and with this problem, the finalization
is never invoked, and the library is ill-finished.

Thank you very much in advance.

PD: I=B4m using Visual C++ 6.0 and Windows 2000
--=20
Antonio Carlos Dom=EDnguez Brito (acdbrito@dis.ulpgc.es)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Carl Daniel" <cpdaniel@pacbell.net>
Date: Fri, 22 Jun 2001 17:50:38 GMT
Raw View
>"Antonio Carlos Dom   nguez Brito" <acdbrito@serdis.dis.ulpgc.es> wrote in
message >news:3B31F005.FB0D799D@serdis.dis.ulpgc.es...
>Well, I hope anybody out there can bring a bit of light here. I think I
>can explain better my problem with an minimum example of code, have a
>look at the following code:

[ code snipped ]

>and here is its output once I've compiled it using Visual C++ 6.0
>
>*** OUTPUT BEGINNING ***
>
>A[_staticA_]::A()
>A[a]::A()
>A[b]::A()
>A[b]::~A()
>
>*** OUTPUT END ***
>
>as you can see here, the destructor of the global object a, and the
>destructor of the static member object of A, _staticA_, are not invoked
>when the application finishes. A priori, I thought it was just a
>question of configuring the compiler, but really before looking around
>on the compiler help, and on the web, I couldn   t find anything to
>"configure" the compiler conveniently. I don   t think is a problem of the
>compiler, because I think it would a violation of C++ standard, so I
>suppose I didn   t look at the correct places. Does anybody knows anything
>more conclusive about the topic? The thing is that I   m using a static
>member object to intialize and finalize a library, using the constructor
>and the destructor of a class, and with this problem, the finalization
>is never invoked, and the library is ill-finished.

Whatever disparaging remarks people may make about VC, one thing few deny is
that is has a great debugger (IMHO the best I've seen).  Try it out
sometimes!  A quick setting of a breakpoint on A::~A reveals the answer:
A[_staticA_] and A[a] were destructed AFTER std::cout, which is also a
static object.  The destructors ran - you just didn't see anything on your
console.  Change the cout's to use printf() and you'll see the same thing.

-cd





---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Attila Feher <Attila.Feher@lmf.ericsson.se>
Date: Fri, 22 Jun 2001 17:50:50 GMT
Raw View
Antonio Carlos Dom=EDnguez Brito wrote:
>=20
> Well, I hope anybody out there can bring a bit of light here. I think I
> can explain better my problem with an minimum example of code, have a
> look at the following code:
[SNIP]
> A[_staticA_]::A()
> A[a]::A()
> A[b]::A()
> A[b]::~A()
>=20
> *** OUTPUT END ***
>=20
> as you can see here, the destructor of the global object a, and the
> destructor of the static member object of A, _staticA_, are not invoked
> when the application finishes. A priori, I thought it was just a
[SNIP]

Can it be so that cout is destroyed before them?  So it does write it
out, but...  Try to write to a file there and look at the file.  It can
be a static member of the class for easiness and safety (not global!).=20
Try to write to disk file (or even to CON:) and see if the msgs are
there.

A

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: pinzo@lettera.net (Bobo)
Date: Fri, 22 Jun 2001 17:52:40 GMT
Raw View
The standard says (if I can remember) that global objects are createn
within a module in the order they are declared, and destructed
inversely.
But the order between modules is undefined.
Since the A global variables are in one module and the cout stream is
created in another, you can't rely in cout existence in the A
destructor.

Maybe using atexit functions to destroy manually the objects.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]