Initialisation Order
- ...
- basic_ios
- logoutbuf
- logoutbuf_init
- basic_ostream
- logostream
Therefore the constructors of basic_ios and outbuf are both called before the constructor of basic_ostream and therefore before basic_ios::init is called. This means that if the outbuf constructor throws an exception, basic_ios's destructor will be called before basic_ios::init; resulting in the undefined behaviour described in 27.4.4/2.
The answer to this problem is contained within 12.6.2/5 and is very simple. Making ostream inherit virtually, as well as privately, from outbuf_init causes it to be constructed before anything else: