added correct memory leak code and explanation