Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

predefined streams flushed at exit? (Was: Re: Trouble running a Dec-Alpha C++ linked with Rational Ada

0 views
Skip to first unread message

Wil Evers

unread,
Dec 13, 1995, 3:00:00 AM12/13/95
to

In article <KANZE.95D...@slsvewt.lts.sel.alcatel.de>
ka...@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763) writes:

> (The current public draft is singularly silent on the
> question of when, or even if, the predefined streams are flushed. It
> does state that the stream objects are never destructed, which would
> mean that they are probably never closed.)

There is a description of a class called ios_base::Init with a static
integer member called init_cnt. The April 28 draft says (27.4.3.1.6,
[lib.ios::Init])

Init();
Effects: Constructs an object of class Init. If init_cnt is zero,
the function stores the value one in init_cnt, then constructs and
initializes the objects cin, cout, cerr, clog (27.3.1), win, wout, werr
and wlog (27.3.2). In any case, the function then adds one to the value
stored in init_cnt.

~Init();
Effects: Destroys an object of class Init. The function subtracts
one from the value stored in init_cnt and, if the resulting stored value
is one, calls cout.flush(), cerr.flush(), and clog.flush().

IMHO there are many things missings here, and I don't know if they have
been fixed in later releases of the draft.

Firstly, I haven't been able to find a header file that declares a static
object of class ios_base::Init - I would expect that to be in <iostream>.
27.3 [lib.iostream.objects] says that [cw]in [cw]out, [cw]err and [cw]log
are initialized the first time an object of a class called
basic_ios<charT, traits>::Init is constructed, but I couldn't find any
further reference to that class. Since the basic_ios template inherits
from ios_base, I'm assuming ios_base::Init is meant here.

Secondly, it seems that wout, werr and wlog are not flushed when
ios_base::Init::init_cnt drops back to one.

Finally, since class ios_base::Init will have a compiler-generated copy
constructor doing nothing, it's very easy to force a premature flush by
simply copy-constructing an temporary object of that class.

Am I missing something? Does anyone know if the issues I mention here have
been fixed in a subsequent draft release?

- Wil Evers (w...@ittpub.nl)


---
[ comp.std.c++ is moderated. Submission address: std...@ncar.ucar.edu.
Contact address: std-c++...@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]

James Kanze US/ESC 60/3/141 #40763

unread,
Dec 13, 1995, 3:00:00 AM12/13/95
to

In article <1995Dec12.110202.1688@ittpub> w...@ittpub.nl (Wil Evers)
writes:

|> In article <KANZE.95D...@slsvewt.lts.sel.alcatel.de>
|> ka...@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763) writes:

|> > (The current public draft is singularly silent on the
|> > question of when, or even if, the predefined streams are flushed. It
|> > does state that the stream objects are never destructed, which would
|> > mean that they are probably never closed.)

|> There is a description of a class called ios_base::Init with a static
|> integer member called init_cnt. The April 28 draft says (27.4.3.1.6,
|> [lib.ios::Init])

|> Init();
|> Effects: Constructs an object of class Init. If init_cnt is zero,
|> the function stores the value one in init_cnt, then constructs and
|> initializes the objects cin, cout, cerr, clog (27.3.1), win, wout, werr
|> and wlog (27.3.2). In any case, the function then adds one to the value
|> stored in init_cnt.

|> ~Init();
|> Effects: Destroys an object of class Init. The function subtracts
|> one from the value stored in init_cnt and, if the resulting stored value
|> is one, calls cout.flush(), cerr.flush(), and clog.flush().

This will generally help, but I'm not sure that it is sufficient in
the general case. If a destructor of a static object tries to use
cout, for example, what is the guarantee that there is still one
ios::Init object left to be destructed (and thus trigger the flush)?
I don't think that there is one.

Of course, if the cout usage is directly in the destructor, I would
probably just declare a local ios::Init in the destructor, to be on
the safe side (or just call cout.flush() directly). The problem
occurs when the destructor calls another function which uses cout;
presumably, this function doesn't know that it will be called from a
destructor of a static object, and will probably not have taken any
precautions.

--
James Kanze Tel.: (+33) 88 14 49 00 email: ka...@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, études et réalisations en logiciel orienté objet --
-- A la recherche d'une activité dans une region francophone

0 new messages