From 1ca79fa49743bf73756b8f2a966a4e1e350f4749 Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Wed, 25 Apr 2018 18:51:28 +0200 Subject: [PATCH] Check own subscriptions when assigning in Subscriptor --- source/base/subscriptor.cc | 2 + tests/base/assign_subscriptor.cc | 88 ++++++++++++++++++++++++++++ tests/base/assign_subscriptor.output | 34 +++++++++++ 3 files changed, 124 insertions(+) create mode 100644 tests/base/assign_subscriptor.cc create mode 100644 tests/base/assign_subscriptor.output diff --git a/source/base/subscriptor.cc b/source/base/subscriptor.cc index 07aad6d59a..631d5e6665 100644 --- a/source/base/subscriptor.cc +++ b/source/base/subscriptor.cc @@ -129,6 +129,7 @@ void Subscriptor::check_no_subscribers () const Subscriptor &Subscriptor::operator = (const Subscriptor &s) { + check_no_subscribers(); object_info = s.object_info; return *this; } @@ -137,6 +138,7 @@ Subscriptor &Subscriptor::operator = (const Subscriptor &s) Subscriptor &Subscriptor::operator = (Subscriptor &&s) noexcept { + check_no_subscribers(); s.check_no_subscribers(); object_info = s.object_info; return *this; diff --git a/tests/base/assign_subscriptor.cc b/tests/base/assign_subscriptor.cc new file mode 100644 index 0000000000..25c89f196f --- /dev/null +++ b/tests/base/assign_subscriptor.cc @@ -0,0 +1,88 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2018 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + + +// check that Subscriptor objects need to be empty before assigning. + + +#include "../tests.h" +#include +#include +#include +#include + +class Test : public Subscriptor +{}; + +int main() +{ + deal_II_exceptions::disable_abort_on_exception(); + + initlog(); + + // should work + { + Subscriptor subscriptor_1; + Subscriptor subscriptor_2; + + SmartPointer smart (&subscriptor_1); + + subscriptor_2 = subscriptor_1; + } + + try + { + Subscriptor subscriptor_1; + Subscriptor subscriptor_2; + + SmartPointer smart (&subscriptor_2); + + subscriptor_2 = subscriptor_1; + } + catch (ExceptionBase &e) + { + deallog << e.get_exc_name() << std::endl; + } + + try + { + Subscriptor subscriptor_1; + Subscriptor subscriptor_2; + + SmartPointer smart (&subscriptor_1); + + subscriptor_2 = std::move(subscriptor_1); + } + catch (ExceptionBase &e) + { + deallog << e.get_exc_name() << std::endl; + } + + try + { + Subscriptor subscriptor_1; + Subscriptor subscriptor_2; + + SmartPointer smart (&subscriptor_2); + + subscriptor_2 = std::move(subscriptor_1); + } + catch (ExceptionBase &e) + { + deallog << e.get_exc_name() << std::endl; + } +} + diff --git a/tests/base/assign_subscriptor.output b/tests/base/assign_subscriptor.output new file mode 100644 index 0000000000..e2b839e9e0 --- /dev/null +++ b/tests/base/assign_subscriptor.output @@ -0,0 +1,34 @@ + +DEAL::Exception: ExcInUse (counter, object_info->name(), infostring) +DEAL:: +-------------------------------------------------------- +An error occurred in file in function + void dealii::Subscriptor::check_no_subscribers() const +The violated condition was: + counter == 0 +Additional information: + (none) +-------------------------------------------------------- + +DEAL::Exception: ExcInUse (counter, object_info->name(), infostring) +DEAL:: +-------------------------------------------------------- +An error occurred in file in function + void dealii::Subscriptor::check_no_subscribers() const +The violated condition was: + counter == 0 +Additional information: + (none) +-------------------------------------------------------- + +DEAL::Exception: ExcInUse (counter, object_info->name(), infostring) +DEAL:: +-------------------------------------------------------- +An error occurred in file in function + void dealii::Subscriptor::check_no_subscribers() const +The violated condition was: + counter == 0 +Additional information: + (none) +-------------------------------------------------------- + -- 2.39.5