Skip to content

Commit 5af4570

Browse files
committed
DirichletBoundaries should be a vector of unique_ptrs
DirichletBoundaries was previously responsible for deleting, but not allocating, the pointers that it held, which made understanding the ownership semantics a bit challenging and caused us to have to define a non-trivial destructor (in dof_map_constraints.C).
1 parent babff94 commit 5af4570

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

include/base/dirichlet_boundaries.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,11 @@ class DirichletBoundary
211211
* \note \p std::map has no virtual destructor, so downcasting here
212212
* would be dangerous.
213213
*/
214-
class DirichletBoundaries : public std::vector<DirichletBoundary *>
214+
class DirichletBoundaries : public std::vector<std::unique_ptr<DirichletBoundary>>
215215
{
216216
public:
217-
DirichletBoundaries() {}
218-
219-
~DirichletBoundaries();
217+
DirichletBoundaries() = default;
218+
~DirichletBoundaries() = default;
220219
};
221220

222221
} // namespace libMesh

src/base/dof_map_constraints.C

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,7 +1585,7 @@ public:
15851585

15861586
// Construct mapping from boundary_id -> (dirichlet_id, DirichletBoundary)
15871587
for (const auto & b_id : dirichlet->b)
1588-
boundary_id_to_ordered_dirichlet_boundaries[b_id].emplace(dirichlet_id, dirichlet);
1588+
boundary_id_to_ordered_dirichlet_boundaries[b_id].emplace(dirichlet_id, dirichlet.get());
15891589

15901590
for (const auto & var : dirichlet->variables)
15911591
{
@@ -5238,7 +5238,7 @@ void DofMap::constrain_p_dofs (unsigned int var,
52385238
#ifdef LIBMESH_ENABLE_DIRICHLET
52395239
void DofMap::add_dirichlet_boundary (const DirichletBoundary & dirichlet_boundary)
52405240
{
5241-
_dirichlet_boundaries->push_back(new DirichletBoundary(dirichlet_boundary));
5241+
_dirichlet_boundaries->push_back(std::make_unique<DirichletBoundary>(dirichlet_boundary));
52425242
}
52435243

52445244

@@ -5250,8 +5250,9 @@ void DofMap::add_adjoint_dirichlet_boundary (const DirichletBoundary & dirichlet
52505250
for (unsigned int i = old_size; i <= qoi_index; ++i)
52515251
_adjoint_dirichlet_boundaries.push_back(new DirichletBoundaries());
52525252

5253+
// Make copy of DirichletBoundary, owned by _adjoint_dirichlet_boundaries
52535254
_adjoint_dirichlet_boundaries[qoi_index]->push_back
5254-
(new DirichletBoundary(dirichlet_boundary));
5255+
(std::make_unique<DirichletBoundary>(dirichlet_boundary));
52555256
}
52565257

52575258

@@ -5287,14 +5288,13 @@ DofMap::get_adjoint_dirichlet_boundaries(unsigned int q)
52875288
void DofMap::remove_dirichlet_boundary (const DirichletBoundary & boundary_to_remove)
52885289
{
52895290
// Find a boundary condition matching the one to be removed
5290-
auto lam = [&boundary_to_remove](const DirichletBoundary * bdy)
5291+
auto lam = [&boundary_to_remove](const auto & bdy)
52915292
{return bdy->b == boundary_to_remove.b && bdy->variables == boundary_to_remove.variables;};
52925293

52935294
auto it = std::find_if(_dirichlet_boundaries->begin(), _dirichlet_boundaries->end(), lam);
52945295

5295-
// Delete it and remove it
5296+
// Assert it was actually found and remove it from the vector
52965297
libmesh_assert (it != _dirichlet_boundaries->end());
5297-
delete *it;
52985298
_dirichlet_boundaries->erase(it);
52995299
}
53005300

@@ -5305,26 +5305,19 @@ void DofMap::remove_adjoint_dirichlet_boundary (const DirichletBoundary & bounda
53055305
libmesh_assert_greater(_adjoint_dirichlet_boundaries.size(),
53065306
qoi_index);
53075307

5308-
auto lam = [&boundary_to_remove](const DirichletBoundary * bdy)
5308+
auto lam = [&boundary_to_remove](const auto & bdy)
53095309
{return bdy->b == boundary_to_remove.b && bdy->variables == boundary_to_remove.variables;};
53105310

53115311
auto it = std::find_if(_adjoint_dirichlet_boundaries[qoi_index]->begin(),
53125312
_adjoint_dirichlet_boundaries[qoi_index]->end(),
53135313
lam);
53145314

5315-
// Delete it and remove it
5315+
// Assert it was actually found and remove it from the vector
53165316
libmesh_assert (it != _adjoint_dirichlet_boundaries[qoi_index]->end());
5317-
delete *it;
53185317
_adjoint_dirichlet_boundaries[qoi_index]->erase(it);
53195318
}
53205319

53215320

5322-
DirichletBoundaries::~DirichletBoundaries()
5323-
{
5324-
for (auto & item : *this)
5325-
delete item;
5326-
}
5327-
53285321
void DofMap::check_dirichlet_bcid_consistency (const MeshBase & mesh,
53295322
const DirichletBoundary & boundary) const
53305323
{

0 commit comments

Comments
 (0)