Fix invalid free() calls triggered by garbage collection
Additional checks on arguments