]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Decrease counters in Subscriptor only upon success 7047/head
authorDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Sat, 11 Aug 2018 19:35:26 +0000 (21:35 +0200)
committerDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Sat, 11 Aug 2018 19:43:42 +0000 (21:43 +0200)
source/base/subscriptor.cc
tests/base/unsubscribe_subscriptor.cc [new file with mode: 0644]
tests/base/unsubscribe_subscriptor.debug.output [new file with mode: 0644]

index 6da08803dfbf3638364b0c127202ac6a7279e13e..57d0615c701a03fa4136eb4dcbf1766e8bdf5a25 100644 (file)
@@ -185,18 +185,22 @@ Subscriptor::unsubscribe(const char *id) const
   if (counter == 0)
     return;
 
-  --counter;
-
 #ifdef DEAL_II_WITH_THREADS
   std::lock_guard<std::mutex> lock(mutex);
 #endif
 
   map_iterator it = counter_map.find(name);
-  AssertNothrow(it != counter_map.end(),
-                ExcNoSubscriber(object_info->name(), name));
-  AssertNothrow(it->second > 0, ExcNoSubscriber(object_info->name(), name));
-
-  it->second--;
+  if (it == counter_map.end() || it->second == 0)
+    {
+      AssertNothrow(it != counter_map.end(),
+                    ExcNoSubscriber(object_info->name(), name));
+      AssertNothrow(it->second > 0, ExcNoSubscriber(object_info->name(), name));
+    }
+  else
+    {
+      --counter;
+      it->second--;
+    }
 }
 
 
diff --git a/tests/base/unsubscribe_subscriptor.cc b/tests/base/unsubscribe_subscriptor.cc
new file mode 100644 (file)
index 0000000..84bc748
--- /dev/null
@@ -0,0 +1,45 @@
+// ---------------------------------------------------------------------
+//
+// 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.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+
+
+// check that unsubscribing with a wrong id is handled correctly
+
+
+#include <deal.II/base/smartpointer.h>
+#include <deal.II/base/subscriptor.h>
+
+#include <iostream>
+#include <vector>
+
+#include "../tests.h"
+
+class Test : public Subscriptor
+{};
+
+int
+main()
+{
+  deal_II_exceptions::disable_abort_on_exception();
+
+  initlog();
+
+  Subscriptor subscriptor;
+  subscriptor.subscribe("a");
+  subscriptor.unsubscribe("b");
+  subscriptor.unsubscribe("a");
+
+  return 0;
+}
diff --git a/tests/base/unsubscribe_subscriptor.debug.output b/tests/base/unsubscribe_subscriptor.debug.output
new file mode 100644 (file)
index 0000000..c2fb9ad
--- /dev/null
@@ -0,0 +1,12 @@
+
+DEAL::Exception: ExcNoSubscriber(object_info->name(), name)
+DEAL::
+--------------------------------------------------------
+An error occurred in file <subscriptor.cc> in function
+    void dealii::Subscriptor::unsubscribe(const char*) const
+The violated condition was: 
+    it != counter_map.end()
+Additional information: 
+    No subscriber with identifier <b> subscribes to this object of class N6dealii11SubscriptorE. Consequently, it cannot be unsubscribed.
+--------------------------------------------------------
+

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.