Fix the problem that memory is accessed after released due to invalid type-cast