diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistry.C b/src/OpenFOAM/db/objectRegistry/objectRegistry.C index 264add34cd..fc3c4c8651 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistry.C +++ b/src/OpenFOAM/db/objectRegistry/objectRegistry.C @@ -334,6 +334,14 @@ bool Foam::objectRegistry::checkOut(const word& key) const void Foam::objectRegistry::clear() { // Free anything owned by the registry + // This needs to be done in two stages: + // - collect objects-to-be-removed + // - actually delete objects + // since the destructor of the regIOobject will actually delete its + // entry from the objectRegistry which messes up the iterator. + + DynamicList owned; + for (iterator iter = begin(); iter != end(); ++iter) { regIOobject* ptr = iter.val(); @@ -341,17 +349,25 @@ void Foam::objectRegistry::clear() if (ptr && ptr->ownedByRegistry()) { // TBD: may wish to have ptr->clearWatches(); - if (objectRegistry::debug) { Pout<< "objectRegistry::clear : " << ptr->name() << " watches :" << flatOutput(ptr->watchIndices()) << nl; } - delete ptr; + + owned.append(ptr); } } + for (regIOobject* objectPtr : owned) + { + // Make sure that the destructor of the regIOobject does a checkout + objectPtr->release(); + + delete objectPtr; + } + HashTable::clear(); }