Grupuri de știri: comp.lang.c++.moderated
De la: jtorjo2...@yahoo.com
Data: Fri, 18 Jan 2008 16:52:27 CST
Local: Sam 19 ian. 2008 00:52
Subiect: destructing of global objects
Hi all,
I've looked at the standard, but it doesn't seem to address this. Assume I have a global object, and it gets destroyed. And I have struct logger { }; Can I safely assume that, after the logger is destroyed, "is_destroyed = true"? I want to *know* if a logger is used after it's been destroyed. So the question is: after the *global* object is destroyed, will its In my case, loggers are always global objects. Best, -- Trebuie să vă conectați înainte de a putea posta mesaje.
Pentru a posta un mesaj, trebuie mai întâi să vă înscrieți în acest grup.
Înainte de a posta, vă rugăm să actualizați porecla pe pagina setări de subscripție.
Nu aveți permisiunea necesară pentru a posta.
| ||||||||||||||
Grupuri de știri: comp.lang.c++.moderated
De la: Ramsey Stone <ram...@euphidime.com>
Data: Sat, 19 Jan 2008 04:15:18 CST
Local: Sam 19 ian. 2008 12:15
Subiect: Re: destructing of global objects
On Jan 18, 2:52 pm, jtorjo2...@yahoo.com wrote:
> Can I safely assume that, after the logger is destroyed, "is_destroyed Absolutely not. > = true"? > I want to *know* if a logger is used after it's been destroyed. You can't do anything to the object after you've deleted it. (I'm > So the question is: after the *global* object is destroyed, will its assuming that you're allocating your loggers via new, how else would a global object be destructed before the program terminates?) I would recommend setting the pointer to null after you've destroyed it, and use that to check. -- Trebuie să vă conectați înainte de a putea posta mesaje.
Pentru a posta un mesaj, trebuie mai întâi să vă înscrieți în acest grup.
Înainte de a posta, vă rugăm să actualizați porecla pe pagina setări de subscripție.
Nu aveți permisiunea necesară pentru a posta.
| ||||||||||||||
Grupuri de știri: comp.lang.c++.moderated
De la: jtorjo2...@yahoo.com
Data: Sun, 20 Jan 2008 20:23:35 CST
Local: Lun 21 ian. 2008 04:23
Subiect: Re: destructing of global objects
On 19 Ian, 12:15, Ramsey Stone <ram...@euphidime.com> wrote:
> On Jan 18, 2:52 pm, jtorjo2...@yahoo.com wrote: I have not deleted it, I don't allocate it using new. > > Can I safely assume that, after the logger is destroyed, "is_destroyed > Absolutely not. > > I want to *know* if a logger is used after it's been destroyed. > > So the question is: after the *global* object is destroyed, will its > You can't do anything to the object after you've deleted it. (I'm > assuming that you're allocating your loggers via new, how else would a That doesn't work. > global object be destructed before the program terminates?) I would > recommend setting the pointer to null after you've destroyed it, and > use that to check. The scenario is like this: - I have a global logger, defined in Translation Unit 1 - I have another global object, defined in Translation Unit 2. Between translation units, it's unspecified which object is Assume the global logger is destroyed first, and that the other global There you go - you have an object used after it's been destroyed. Best, -- Trebuie să vă conectați înainte de a putea posta mesaje.
Pentru a posta un mesaj, trebuie mai întâi să vă înscrieți în acest grup.
Înainte de a posta, vă rugăm să actualizați porecla pe pagina setări de subscripție.
Nu aveți permisiunea necesară pentru a posta.
| ||||||||||||||
Grupuri de știri: comp.lang.c++.moderated
De la: ap...@student.open.ac.uk
Data: Mon, 21 Jan 2008 08:51:34 CST
Local: Lun 21 ian. 2008 16:51
Subiect: Re: destructing of global objects
On 21 Jan, 02:23, jtorjo2...@yahoo.com wrote:
Actually, with a slight modification this can be made to work. I have exactly the same problem with a logger of mine a few years ago. I provided an accessor routine to return the address of the logger object. This was done by having a pointer that was initialized to the address of the logger variable. Instead of setting destroyed to true I set this pointer to null. Access was always and only via the accessor method that returned the pointer so this enabled me to detect when the accessor was returning a pointer to a destroyed logger. I really don't like singletons and now handle logging differently Regards, Andrew Marlow -- Trebuie să vă conectați înainte de a putea posta mesaje.
Pentru a posta un mesaj, trebuie mai întâi să vă înscrieți în acest grup.
Înainte de a posta, vă rugăm să actualizați porecla pe pagina setări de subscripție.
Nu aveți permisiunea necesară pentru a posta.
| ||||||||||||||
Grupuri de știri: comp.lang.c++.moderated
De la: jtorjo2...@yahoo.com
Data: Mon, 21 Jan 2008 13:52:19 CST
Local: Lun 21 ian. 2008 21:52
Subiect: Re: destructing of global objects
> Actually, with a slight modification this can be made to work. I have > I really don't like singletons and now handle logging differently to global objects. The order of inialization between translation units is not defined, You just can't guard against this ;) Best, -- -- Trebuie să vă conectați înainte de a putea posta mesaje.
Pentru a posta un mesaj, trebuie mai întâi să vă înscrieți în acest grup.
Înainte de a posta, vă rugăm să actualizați porecla pe pagina setări de subscripție.
Nu aveți permisiunea necesară pentru a posta.
| ||||||||||||||
Grupuri de știri: comp.lang.c++.moderated
De la: "Daniel Krügler" <daniel.krueg...@googlemail.com>
Data: Mon, 21 Jan 2008 15:55:33 CST
Local: Lun 21 ian. 2008 23:55
Subiect: Re: destructing of global objects
On 21 Jan., 20:52, jtorjo2...@yahoo.com wrote:
> The order of inialization between translation units is not defined, You could use the approach that good ol' > thus the same applies > for destruction. The following can happen: > - a global object is constructed before a logger > - thus, it will be destroyed after the logger > - if in its destructor it tries to use the logger, there we go - > logger is used after it's been destroyed. > You just can't guard against this ;) std::ios_base::Init has been invented for because the global IO inserter/extractors (cin, cout, ...) have essentially the same problem which you are describing. Todays library writer can do better with non-portable means, but the idea behind std::ios_base::Init works in most cases (I have heard, that you can subvert it somehow, but I forgot how ;-). The idea is as follows: Define in your header, that declares your -------------------------- class logger { }; class logger::init { static int count; public: init(); // .cpp ~init(); // .cpp }; namespace { // Each including translation file will have an instance of // logger::init, therefore we use unnamed ns to prevent ODR. logger::init dont_touch_me_or_ill_bite_you; } // .cpp: int logger::init::count = 0; // Static initialization logger::init::init() { } logger::init::~init() { if (--count == 0) { // Cleanup logger } } -------------------------- Since each other global variable which HTH & Greetings from Bremen, Daniel Krügler -- Trebuie să vă conectați înainte de a putea posta mesaje.
Pentru a posta un mesaj, trebuie mai întâi să vă înscrieți în acest grup.
Înainte de a posta, vă rugăm să actualizați porecla pe pagina setări de subscripție.
Nu aveți permisiunea necesară pentru a posta.
| ||||||||||||||
Grupuri de știri: comp.lang.c++.moderated
De la: jtorjo2...@yahoo.com
Data: Tue, 22 Jan 2008 05:15:44 CST
Local: Mar 22 ian. 2008 13:15
Subiect: Re: destructing of global objects
> Since each other global variable which TU = Translation Unit Assume we have TU1 TU2 TU3 So the solution should be: if in a translation unit you have a static Best, -- -- Trebuie să vă conectați înainte de a putea posta mesaje.
Pentru a posta un mesaj, trebuie mai întâi să vă înscrieți în acest grup.
Înainte de a posta, vă rugăm să actualizați porecla pe pagina setări de subscripție.
Nu aveți permisiunea necesară pentru a posta.
| ||||||||||||||
Grupuri de știri: comp.lang.c++.moderated
De la: "Daniel Krügler" <daniel.krueg...@googlemail.com>
Data: Tue, 22 Jan 2008 13:42:04 CST
Local: Mar 22 ian. 2008 21:42
Subiect: Re: destructing of global objects
On 22 Jan., 12:15, jtorjo2...@yahoo.com wrote:
Right. Or you use Jason Hise's advise, which is fixes this situation. If we translate Jason's approach in the Init-idea, this simply means that the class declaration of A already needs to include the logger header. Essentially he's doing the same thing, because by requiring depends_on to be a base class of the dependee is equivalent to require that it's template parameter (which is logger) is complete at this point. I see one advantage and one disadvantage +: It doesn't require dynamic initialization Fortunately the last point has lesser weight IMO his depends_on approach is very Greetings from Bremen, Daniel -- Trebuie să vă conectați înainte de a putea posta mesaje.
Pentru a posta un mesaj, trebuie mai întâi să vă înscrieți în acest grup.
Înainte de a posta, vă rugăm să actualizați porecla pe pagina setări de subscripție.
Nu aveți permisiunea necesară pentru a posta.
| ||||||||||||||
Grupuri de știri: comp.lang.c++.moderated
De la: "Daniel Krügler" <daniel.krueg...@googlemail.com>
Data: Tue, 22 Jan 2008 22:33:00 CST
Subiect: Re: destructing of global objects
On 22 Jan., 20:42, "Daniel Krügler" <daniel.krueg...@googlemail.com>
wrote: > I see one advantage and one disadvantage NB: A data membership would also suffice, but > of depends on: > +: It doesn't require dynamic initialization has (in this case) the same form of dependency (namely completeness) and has lesser advantages for EBCO. Greetings from Bremen, Daniel -- Trebuie să vă conectați înainte de a putea posta mesaje.
Pentru a posta un mesaj, trebuie mai întâi să vă înscrieți în acest grup.
Înainte de a posta, vă rugăm să actualizați porecla pe pagina setări de subscripție.
Nu aveți permisiunea necesară pentru a posta.
| ||||||||||||||
Grupuri de știri: comp.lang.c++.moderated
De la: Jason Hise <0xCH...@gmail.com>
Data: Tue, 22 Jan 2008 05:21:57 CST
Local: Mar 22 ian. 2008 13:21
Subiect: Re: destructing of global objects
On Jan 21, 3:55 pm, "Daniel Krügler" <daniel.krueg...@googlemail.com>
wrote: There is another alternative which is supported by the standard. If you use function local static variables for your instances, and can make sure that instances which use the logger always access the logger in some way during construction, it will guarantee that the logger has a longer lifespan, even if the instances live in different translation units. See www.entropygames.net/globals.htm for a more detailed explanation. -- Trebuie să vă conectați înainte de a putea posta mesaje.
Pentru a posta un mesaj, trebuie mai întâi să vă înscrieți în acest grup.
Înainte de a posta, vă rugăm să actualizați porecla pe pagina setări de subscripție.
Nu aveți permisiunea necesară pentru a posta.
| ||||||||||||||