fix free() on invalid pointer