On the residual finiteness of generalized free products