fix memleaks bugs