the previous fix for double free was wrong. so here is another fix