Moved mem_safe_free to avoid compiler bug