Fix memory leaks on opening and closing a project