You are correct about my bug but the required fix is slightly different.
The correct code is:
if (invalidParent || (grandpaRef && !m_nav->isValidPolyRef(grandpaRef)) )

Your suggested code will stop even if grandpaRef is 0 (which happens all the time after the first node). The required behavior is to stop if there is a ref but the node got deleted between the slice calculations.