[gnu.g++.lib.bug] Bugfix for Files.cc

tietz@gmdzi.UUCP (Christoph Tietz) (08/23/89)

I am working on a SUN3 running SUNOS 4.0 with g++ 1.35.1- and libg++ 1.35.1

Problem:
If you want to do standard I/O during cleanup of static objects around main()
you get in trouble. The problem is, that the destruction of an istream or
ostream for standard I/O calls (over the Filebuf destructor) the close method
of the corresponding file class. This is disastrous for the standard I/O
because it is now no longer closed as the last action of your program. In
fact it is closed at some (early) time during the global cleanup.  This
cleanup might try to produce some output afterwards that is never seen.

The destructor for the File class tests on standard I/O and only performs a
flush if stdin, stdout or stderr are found. If you call the File::close
method directly, no test on standard I/O is done.

Fix:
I moved the test on standard I/O from ~File to File::close. The diff for
File.cc follows:

205,206d204
<     else if (fp == stdin || fp == stdout || fp == stderr)
<       flush();
285c283,286
<   close();
---
>   if (fp == stdin || fp == stdout || fp == stderr)
>     flush();
>   else if (fp != 0)
>     close();


Now even the following program works as expected:

-----------------------------------------------------------------
#include <stream.h>

ostream	sout(1);	// standard ostream for init and cleanup

class dummy {
public:
  dummy()	{sout << "Initialization\n"; sout.flush();}
  ~dummy()	{sout << "Clean Up\n";}
};

dummy A;

main() { 
  cout << "Hello, world!\n";
}

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

I hope, I fixed a bug and not a feature...

I am aware of the fact, that the global cleanup cannot be guaranteed to be
done before standard I/O is closed on some machines. The code above will not
work on those machines. So, at best, I have fixed a nonportable feature ;-)

                                            Christoph Tietz

Gesellschaft fuer Mathematik       e-mail: tietz@zi.gmd.dbp.de
  und Datenverarbeitung                    tietz@gmdzi.uucp
Schloss Birlinghoven                       tietz%zi.gmd.dbp.de@relay.cs.net
Postfach 1240                              ... !uunet!unido!gmdzi!tietz
D-5205 St. Augustin 1
Federal Republic of Germany