Fixed invalid free() and Python API