fix mem leak and empty check