Skip to content

Commit 31ad40b

Browse files
hcho3terrytangyuan
authored andcommitted
Make __del__ method idempotent (#2627)
Addresses Issue #2533.
1 parent 8d15024 commit 31ad40b

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

python-package/xgboost/core.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,9 @@ def _init_from_npy2d(self, mat, missing, nthread):
363363
nthread))
364364

365365
def __del__(self):
366-
_check_call(_LIB.XGDMatrixFree(self.handle))
366+
if self.handle is not None:
367+
_check_call(_LIB.XGDMatrixFree(self.handle))
368+
self.handle = None
367369

368370
def get_float_info(self, field):
369371
"""Get float property from the DMatrix.
@@ -738,7 +740,9 @@ def __init__(self, params=None, cache=(), model_file=None):
738740
self.load_model(model_file)
739741

740742
def __del__(self):
741-
_LIB.XGBoosterFree(self.handle)
743+
if self.handle is not None:
744+
_check_call(_LIB.XGBoosterFree(self.handle))
745+
self.handle = None
742746

743747
def __getstate__(self):
744748
# can't pickle ctypes pointers

0 commit comments

Comments
 (0)