destructing of global objects
semn
Mesaje 1 - 10 din 12 - Restrângere totală
/groups/adfetch?hl=ro&adid=bygXqhEAAAB7lkSon_G_MNXq8ZkD3E7yi_s2p3x1d9mB7p8AMaL9cQ
Grupul în care postați dvs. este un grup Usenet. Mesajele postate în acesta vă vor face adresa de e-mail vizibilă oricărui utilizator de pe Internet.
Mesajul dvs. de răspuns nu a fost trimis.
Postarea a fost efectuată
 
De la:
Către:
Cc:
Răspundeți la:
Adăugați Cc | Adăugați mesaj de revenire | Editați subiectul
Subiect:
Validare:
În scopul verificării, tastați caracterele pe care le vedeți în imaginea de mai jos sau numerele pe care le auziți după ce faceți clic pe pictograma de accesibilitate. Ascultați și tastați numerele pe care le auziți
 
1.  jtorjo2...@yahoo.com  
Vizualizați profilul   Traduceți în În traducere (Vedeți versiunea originală)
 Mai multe opțiuni 19 ian. 2008, 00:52
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
something like this:

struct logger {
   bool is_destroyed;
   ...
   ~logger() : { is_destroyed = true; }

};

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
memory get overwritten? Or can I still "play" with that memory?

In my case, loggers are always global objects.

Best,
John

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Redirecționați  
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.
2.  Ramsey Stone  
Vizualizați profilul   Traduceți în În traducere (Vedeți versiunea originală)
 Mai multe opțiuni 19 ian. 2008, 12:15
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
> = true"?

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
> memory get overwritten? Or can I still "play" with that memory?

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
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.

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Redirecționați  
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.
3.  jtorjo2...@yahoo.com  
Vizualizați profilul   Traduceți în În traducere (Vedeți versiunea originală)
 Mai multe opțiuni 21 ian. 2008, 04:23
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:

> > Can I safely assume that, after the logger is destroyed, "is_destroyed
> > = true"?

> 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
> > memory get overwritten? Or can I still "play" with that memory?

> You can't do anything to the object after you've deleted it. (I'm

I have not deleted it, I don't allocate it using new.

> 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.

That doesn't work.
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
constructed first, thus, it's not sure which object will be destroyed
first.

Assume the global logger is destroyed first, and that the other global
object references it from its destructor.

There you go - you have an object used after it's been destroyed.

Best,
John

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Redirecționați  
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.
4.  ap...@student.open.ac.uk  
Vizualizați profilul   Traduceți în În traducere (Vedeți versiunea originală)
 Mai multe opțiuni 21 ian. 2008, 16:51
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
using Parameterize From Above (TM). But for those that want to use
singletons I recommend using a singleton manager. An example of one
can be seen in ACE. These managers control creation of singletons so
that they are destroyed in reverse order of their creation. Hopefully
this will help.

Regards,

Andrew Marlow

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Redirecționați  
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.
5.  jtorjo2...@yahoo.com  
Vizualizați profilul   Traduceți în În traducere (Vedeți versiunea originală)
 Mai multe opțiuni 21 ian. 2008, 21:52
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
> 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.

Yes that can work too , anyway it's the same idea.

> I really don't like singletons and now handle logging differently
> using Parameterize From Above (TM). But for those that want to use
> singletons I recommend using a singleton manager. An example of one
> can be seen in ACE. These managers control creation of singletons so
> that they are destroyed in reverse order of their creation. Hopefully
> this will help.

This does not hep - it' not related to singletons at all. It's related
to global objects.

The order of inialization between translation units is not defined,
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 ;)

Best,
John

--
http://John.Torjo.com -- C++ expert
... call me only if you want things done right

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Redirecționați  
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.
6.  Daniel Krügler  
Vizualizați profilul   Traduceți în În traducere (Vedeți versiunea originală)
 Mai multe opțiuni 21 ian. 2008, 23:55
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,
> 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 ;)

You could use the approach that good ol'
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
logger something like this (More or less
stolen from Stroustrup's "The C++ Programming
Language"):

--------------------------
// Header:

class logger {
  ..
public:
  class init;

};

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() {
  if (count++ == 0) {
    // Init logger
  }

}

logger::init::~init() {
  if (--count == 0) {
    // Cleanup logger
  }
}

--------------------------

Since each other global variable which
uses logger needs to include your header,
there will be one logger::init object
*before* the user variable. Inside this
translation unit the logger::init object
will be constructed *before* the other
global variable (if that does not use
static initialization, but in this case,
it will not use your logger), and due
to the ref counter it will only be
constructed *once*. The same logic
applies to the d'tor, but reverse.

HTH & Greetings from Bremen,

Daniel Krügler

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Redirecționați  
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.
7.  jtorjo2...@yahoo.com  
Vizualizați profilul   Traduceți în În traducere (Vedeți versiunea originală)
 Mai multe opțiuni 22 ian. 2008, 13:15
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
> uses logger needs to include your header,
> there will be one logger::init object
> *before* the user variable. Inside this
> translation unit the logger::init object
> will be constructed *before* the other
> global variable (if that does not use
> static initialization, but in this case,
> it will not use your logger), and due
> to the ref counter it will only be
> constructed *once*. The same logic
> applies to the d'tor, but reverse.

Actually, now that I think about it, it's not as easy as I thought.
TU = Translation Unit
Assume we have

TU1
// this includes the header with the logger::init trick
implementation of destructor for class A, which uses logger L

TU2
// this does not include the header with the logger::init trick
static instance of class A

TU3
static instance of logger L

So the solution should be: if in a translation unit you have a static
instance of some class, and in its destructor you might end up doing
logging, you should include the logging header (which will do the
initialization trick)

Best,
John

--
http://John.Torjo.com -- C++ expert
... call me only if you want things done right

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Redirecționați  
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.
8.  Daniel Krügler  
Vizualizați profilul   Traduceți în În traducere (Vedeți versiunea originală)
 Mai multe opțiuni 22 ian. 2008, 21:42
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
of depends on:

+: It doesn't require dynamic initialization
-: It requires a base-derived relation.

Fortunately the last point has lesser weight
if we consider that this base class can(and
probably should) be a private one.

IMO his depends_on approach is very
clever and better than the old Init style.
One should of course note that Meyer's
singleton must be guarded somehow in
multithreading conditions (but the init
approach also).

Greetings from Bremen,

Daniel

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Redirecționați  
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.
9.  Daniel Krügler  
Vizualizați profilul   Traduceți în În traducere (Vedeți versiunea originală)
 Mai multe opțiuni 23 ian. 2008, 06:33
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
> of depends on:

> +: It doesn't require dynamic initialization
> -: It requires a base-derived relation.

NB: A data membership would also suffice, but
has (in this case) the same form of dependency
(namely completeness) and has lesser advantages
for EBCO.

Greetings from Bremen,

Daniel

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Redirecționați  
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.
10.  Jason Hise  
Vizualizați profilul   Traduceți în În traducere (Vedeți versiunea originală)
 Mai multe opțiuni 22 ian. 2008, 13:21
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.

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Redirecționați  
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.

Creați un grup - Grupuri Google - Pagina de pornire Google - Condițiile de furnizare a serviciilor - Politica de confidențialitate
©2010 Google