Memory leak prevention and specific exception