fix some bugs which may lead program to leak mem