diff --git a/gtsam/inference/BayesTreeCliqueBase-inl.h b/gtsam/inference/BayesTreeCliqueBase-inl.h index f96566536..344f845ac 100644 --- a/gtsam/inference/BayesTreeCliqueBase-inl.h +++ b/gtsam/inference/BayesTreeCliqueBase-inl.h @@ -268,12 +268,19 @@ namespace gtsam { /* ************************************************************************* */ template void BayesTreeCliqueBase::deleteCachedShorcuts() { - //Delete CachedShortcut for this clique - this->resetCachedShortcut(); - // Recursive call over all child cliques - BOOST_FOREACH(derived_ptr& child, children_) { - child->deleteCachedShorcuts(); - } + + // When a shortcut is requested, all of the shortcuts between it and the + // root are also generated. So, if this clique's cached shortcut is set, + // recursively call over all child cliques. Otherwise, it is unnecessary. + if(cachedShortcut_) { + BOOST_FOREACH(derived_ptr& child, children_) { + child->deleteCachedShorcuts(); + } + + //Delete CachedShortcut for this clique + this->resetCachedShortcut(); + } + } }