I'm not sure if this will help, but the following will limit the scope of'envelopeFailure'. This may be important if your code is reentrant through'envelopeController.injectEnvelope'. I'm not sure what 'shared_dynamic_cast'is above, but assume you meant 'dynamic_pointer_cast'?if( shared_ptr<EnvelopeFailure> envelopeFailure= dynamic_pointer_cast<EnvelopeFailure>(envelope) ){failedEnvelope( envelopeFailure->getFrom() );}