Skip to content

Commit bfbd68f

Browse files
ax3lWeiqunZhang
andauthored
Fix: Make Finalize->Initialize->F->I->... Work (AMReX-Codes#2944)
Fix assertions in Arena::Initialize. The_BArena never dies (tm) Co-authored-by: Weiqun Zhang <[email protected]>
1 parent 6738470 commit bfbd68f

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Src/Base/AMReX_Arena.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,13 @@ Arena::Initialize ()
253253
if (initialized) return;
254254
initialized = true;
255255

256-
BL_ASSERT(the_arena == nullptr);
256+
// see reason on allowed reuse of the default CPU BArena in Arena::Finalize
257+
BL_ASSERT(the_arena == nullptr || the_arena == The_BArena());
257258
BL_ASSERT(the_async_arena == nullptr);
258-
BL_ASSERT(the_device_arena == nullptr);
259-
BL_ASSERT(the_managed_arena == nullptr);
259+
BL_ASSERT(the_device_arena == nullptr || the_device_arena == The_BArena());
260+
BL_ASSERT(the_managed_arena == nullptr || the_managed_arena == The_BArena());
260261
BL_ASSERT(the_pinned_arena == nullptr);
261-
BL_ASSERT(the_cpu_arena == nullptr);
262+
BL_ASSERT(the_cpu_arena == nullptr || the_cpu_arena == The_BArena());
262263

263264
#ifdef AMREX_USE_GPU
264265
#ifdef AMREX_USE_DPCPP
@@ -468,6 +469,13 @@ Arena::Finalize ()
468469

469470
initialized = false;
470471

472+
// we reset Arenas unless they are the default "CPU malloc/free" BArena
473+
// this is because we want to allow users to free their UB objects
474+
// that they forgot to destruct after amrex::Finalize():
475+
// amrex::Initialize(...);
476+
// MultiFab mf(...); // this should be scoped in { ... }
477+
// amrex::Finalize();
478+
// mf cannot be used now, but it can at least be freed without a segfault
471479
if (!dynamic_cast<BArena*>(the_device_arena)) {
472480
if (the_device_arena != the_arena) {
473481
delete the_device_arena;

0 commit comments

Comments
 (0)