From bd13507954d6d80a4f1ab267831084ff5642fef5 Mon Sep 17 00:00:00 2001 From: bangerth Date: Tue, 18 Sep 2012 02:21:32 +0000 Subject: [PATCH] Fix a crash under rare circumstances at the end of a program. git-svn-id: https://svn.dealii.org/trunk@26463 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/doc/news/changes.h | 8 +++++ deal.II/source/base/logstream.cc | 49 ++++++++++++++++++++++++++++- tests/base/log_crash_01.cc | 8 +++-- tests/base/log_crash_01/cmp/generic | 7 +---- 4 files changed, 63 insertions(+), 9 deletions(-) diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index aeb69157cb..7ed9f6cc97 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -53,6 +53,14 @@ DoFHandler, in particular removal of specializations.

Specific improvements

    +
  1. Fixed: If you pipe content into the deallog object and there +is no end-line or flush after this content, and if a file stream +is associated to this object, and if that happens at the end of +the lifetime of the program, then the program would crash. +This is now fixed. +
    +(Timo Heister, Wolfgang Bangerth, 2012/09/17) +
  2. Fixed: The use of TableHandler::set_precision affected not only the precision with which elements of a table were printed, but also the precision carried by the output stream after writing the table was diff --git a/deal.II/source/base/logstream.cc b/deal.II/source/base/logstream.cc index 98f06e51dd..677e0c0f4c 100644 --- a/deal.II/source/base/logstream.cc +++ b/deal.II/source/base/logstream.cc @@ -81,7 +81,54 @@ LogStream::~LogStream() (*outstreams[id] != 0) && (outstreams[id]->str().length() > 0)) - *this << std::endl; + { + // except the situation is + // not quite that simple. if + // this object is the + // 'deallog' object, then it + // is destroyed upon exit of + // the program. since it's + // defined in a shared + // library that depends on + // libstdc++.so, destruction + // happens before destruction + // of std::cout/cerr, but + // after all file variables + // defined in user programs + // have been destroyed. in + // other words, if we get + // here and the object being + // destroyed is 'deallog' and + // if 'deallog' is associated + // with a file stream, then + // we're in trouble: we'll + // try to write to a file + // that doesn't exist any + // more, and we're likely + // going to crash (this is + // tested by + // base/log_crash_01). rather + // than letting it come to + // this, print a message to + // the screen (note that we + // can't issue an assertion + // here either since Assert + // may want to write to + // 'deallog' itself, and + // AssertThrow will throw an + // exception that can't be + // caught) + if ((this == &deallog) && file) + std::cerr << ("You still have content that was written to 'deallog' " + "but not flushed to the screen or a file while the " + "program is being terminated. This would lead to a " + "segmentation fault. Make sure you flush the " + "content of the 'deallog' object using 'std::endl' " + "before the end of the program.") + << std::endl; + else + *this << std::endl; + } } if (old_cerr) diff --git a/tests/base/log_crash_01.cc b/tests/base/log_crash_01.cc index 4d2fd1c18c..ddeb38d72b 100644 --- a/tests/base/log_crash_01.cc +++ b/tests/base/log_crash_01.cc @@ -1,8 +1,8 @@ //---------------------------- logtest.cc --------------------------- // $Id: logtest.cc 23710 2011-05-17 04:50:10Z bangerth $ -// Version: $Name$ +// Version: $Name$ // -// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007 by the deal.II authors +// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2012 by the deal.II authors // // This file is subject to QPL and may not be distributed // without copyright and license information. Please refer @@ -24,7 +24,11 @@ int main() { + deal_II_exceptions::disable_abort_on_exception (); + std::ofstream logfile("log_crash_01/output"); deallog.attach(logfile); + deallog << "OK" << std::endl; + deallog << "no newline here!"; } diff --git a/tests/base/log_crash_01/cmp/generic b/tests/base/log_crash_01/cmp/generic index 035550ac63..0fd8fc12f0 100644 --- a/tests/base/log_crash_01/cmp/generic +++ b/tests/base/log_crash_01/cmp/generic @@ -1,7 +1,2 @@ -DEAL::Test -DEAL:l1::Test1 -DEAL:l1:l2::Test2Test3 -DEAL:l1:l2::Test4Test5 -DEAL:l1::Test6 -DEAL::Test7 +DEAL::OK -- 2.39.5