From: danshapero Date: Wed, 6 Apr 2016 16:58:10 +0000 (-0700) Subject: Factored out Subscriptor error checking into member function X-Git-Tag: v8.5.0-rc1~1133^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=965200fc47b073f1834983bf296a42b58c5ff9c2;p=dealii.git Factored out Subscriptor error checking into member function --- diff --git a/include/deal.II/base/subscriptor.h b/include/deal.II/base/subscriptor.h index 4ddd83af30..d4fd6d9df7 100644 --- a/include/deal.II/base/subscriptor.h +++ b/include/deal.II/base/subscriptor.h @@ -206,6 +206,13 @@ private: * in between and store it here. */ mutable const std::type_info *object_info; + + /** + * Check that there are no objects subscribing to this object and throw an + * error if there are, in order to guarantee that it is safe to either move + * or destroy this object. + */ + void check_no_subscribers () const; }; //--------------------------------------------------------------------------- diff --git a/source/base/subscriptor.cc b/source/base/subscriptor.cc index 07240b88e9..8711270c9f 100644 --- a/source/base/subscriptor.cc +++ b/source/base/subscriptor.cc @@ -69,10 +69,9 @@ Subscriptor::Subscriptor (Subscriptor &&subscriptor) counter(0), object_info (subscriptor.object_info) { - // Explicitly invoke the destructor of `Subscriptor` for the object - // to be moved from in order to guarantee that we're not moving an - // object that has subscriptions. - subscriptor.~Subscriptor(); + subscriptor.check_no_subscribers(); + subscriptor.counter = 0; + subscriptor.counter_map.clear(); } #endif @@ -80,37 +79,40 @@ Subscriptor::Subscriptor (Subscriptor &&subscriptor) Subscriptor::~Subscriptor () { - // check whether there are still - // subscriptions to this object. if - // so, output the actual name of - // the class to which this object - // belongs, i.e. the most derived - // class. note that the name may be - // mangled, so it need not be the - // clear-text class name. however, - // you can obtain the latter by - // running the c++filt program over - // the output. + check_no_subscribers(); + counter = 0; + counter_map.clear(); + +#ifdef DEAL_II_WITH_CXX11 + object_info = nullptr; +#else + object_info = 0; +#endif +} + + +void Subscriptor::check_no_subscribers () const +{ + // Check whether there are still subscriptions to this object. If so, output + // the actual name of the class to which this object belongs, i.e. the most + // derived class. Note that the name may be mangled, so it need not be the + // clear-text class name. However, you can obtain the latter by running the + // c++filt program over the output. #ifdef DEBUG - // if there are still active pointers, show - // a message and kill the program. However, - // under some circumstances, this is not so - // desirable. For example, in code like this + // If there are still active pointers, show a message and kill the program. + // However, under some circumstances, this is not so desirable. For example, + // in code like this: // - // Triangulation tria; - // DoFHandler *dh = new DoFHandler(tria); - // ...some function that throws an exception + // Triangulation tria; + // DoFHandler *dh = new DoFHandler(tria); + // ...some function that throws an exception // - // the exception will lead to the - // destruction of the triangulation, but - // since the dof_handler is on the heap it - // will not be destroyed. This will trigger - // an assertion in the triangulation. If we - // kill the program at this point, we will - // never be able to learn what caused the - // problem. In this situation, just display - // a message and continue the program. + // the exception will lead to the destruction of the triangulation, but since + // the dof_handler is on the heap it will not be destroyed. This will trigger + // an assertion in the triangulation. If we kill the program at this point, we + // will never be able to learn what caused the problem. In this situation, + // just display a message and continue the program. if (counter != 0) { if (std::uncaught_exception() == false) @@ -147,17 +149,6 @@ Subscriptor::~Subscriptor () << std::endl; } } - // In case we do not abort - // on error, this will tell - // do_unsubscribe below that the - // object is unused now. - counter = 0; - -#ifdef DEAL_II_WITH_CXX11 - object_info = nullptr; -#else - object_info = 0; -#endif #endif } diff --git a/tests/lac/sparse_matrix_move.cc b/tests/lac/sparse_matrix_move.cc index 8e674dde9f..7d15ba4acc 100644 --- a/tests/lac/sparse_matrix_move.cc +++ b/tests/lac/sparse_matrix_move.cc @@ -101,31 +101,5 @@ int main() deallog << A.empty() << std::endl; } - { - // Try to move a sparse matrix that has a subscription - SparseMatrix A = graph_laplacian(sparsity); - SmartPointer > smart_pointer_to_A(&A); - - bool need_to_catch_exc_in_use = false; -#ifdef DEBUG - need_to_catch_exc_in_use = true; -#endif - - try - { - SparseMatrix B = std::move(A); - } - catch (const Subscriptor::ExcInUse &) - { - need_to_catch_exc_in_use = false; - } - catch (...) - { - return 1; - } - - deallog << "Exception caught if in debug mode." << std::endl; - } - return 0; } diff --git a/tests/lac/sparse_matrix_move.with_cxx11=on.output b/tests/lac/sparse_matrix_move.with_cxx11=on.output index ced490f8e7..7239c9cda8 100644 --- a/tests/lac/sparse_matrix_move.with_cxx11=on.output +++ b/tests/lac/sparse_matrix_move.with_cxx11=on.output @@ -5,4 +5,3 @@ DEAL::64 DEAL::0.00 DEAL::16 DEAL::1 -DEAL::Exception caught if in debug mode.