From: Wolfgang Bangerth Date: Thu, 10 Nov 2016 16:15:18 +0000 (-0700) Subject: Avoid a memory leak. X-Git-Tag: v8.5.0-rc1~420^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8035a9d0982025b9ffe9042a44017e7a2b78a480;p=dealii.git Avoid a memory leak. In fact, the test really tries very hard to create a memory leak, and even verifies that it happens. But this then leads to downstream heartbreak if you run a memory checker because you get pulled over for it. Fix the issue by cleaning up the memory leak after we have verified that the code that ran before really did leak the memory, by keeping a pointer to the leaked object after all, and deleting it at the end of it all. --- diff --git a/tests/serialization/pointer_02.cc b/tests/serialization/pointer_02.cc index f10eff49eb..0a8bfa04b5 100644 --- a/tests/serialization/pointer_02.cc +++ b/tests/serialization/pointer_02.cc @@ -1,6 +1,6 @@ // --------------------------------------------------------------------- // -// Copyright (C) 2010 - 2015 by the deal.II authors +// Copyright (C) 2010 - 2016 by the deal.II authors // // This file is part of the deal.II library. // @@ -76,9 +76,11 @@ private: void test () { + C *backup; { C *p1 = new C(); - C *p2 = new C(); + C *p2 = new C(); // this is the pointer that will be overwritten + backup = p2; // but save a pointer to the original object verify (p1, p2); @@ -93,6 +95,11 @@ void test () // original object pointed to as a memory // leak. assert that this behavior persists AssertThrow (objects_destroyed == 2, ExcInternalError()); + + // we've checked what we wanted to check, so now delete + // the original object to ensure we don't get undue + // error messages from memory checkers about leaked memory + delete backup; } diff --git a/tests/serialization/pointer_02.output b/tests/serialization/pointer_02.output index 8b72e9c4ad..11f2edbd0f 100644 --- a/tests/serialization/pointer_02.output +++ b/tests/serialization/pointer_02.output @@ -9,4 +9,5 @@ DEAL::Default constructor. Object number 3 DEAL::Serializing object number 3 via N5boost7archive13text_iarchiveE DEAL::destructor. Object number 1 DEAL::destructor. Object number 3 +DEAL::destructor. Object number 2 DEAL::OK