This should not be using getNumInstances - you are begging for a race condition if there is a difference between the model and the object manager.  Use parent->getChildCount (IIRC).  Check my previous comment.