diff --git a/python/xgboost.py b/python/xgboost.py index 0e8d8fb0b..0cfb47269 100644 --- a/python/xgboost.py +++ b/python/xgboost.py @@ -96,6 +96,8 @@ class Booster: dmats = ( ctypes.c_void_p * len(cache) )(*[ ctypes.c_void_p(d.handle) for d in cache]) self.handle = xglib.XGBoosterCreate( dmats, len(cache) ) self.set_param( params ) + def __del__(self): + xglib.XGBoosterFree(self.handle) def set_param(self, params,pv=None): if isinstance(params,dict): for k, v in params.iteritems(): diff --git a/python/xgboost_python.cpp b/python/xgboost_python.cpp index b02f7d8ea..ec1b75e49 100644 --- a/python/xgboost_python.cpp +++ b/python/xgboost_python.cpp @@ -170,6 +170,9 @@ extern "C"{ } return new Booster( mats ); } + void XGBoosterFree( void *handle ){ + delete static_cast(handle); + } void XGBoosterSetParam( void *handle, const char *name, const char *value ){ static_cast(handle)->SetParam( name, value ); } diff --git a/python/xgboost_python.h b/python/xgboost_python.h index 351a7ad6b..cbde50766 100644 --- a/python/xgboost_python.h +++ b/python/xgboost_python.h @@ -109,6 +109,11 @@ extern "C"{ * \param create a booster */ void *XGBoosterCreate( void* dmats[], size_t len ); + /*! + * \brief free obj in handle + * \param handle handle to be freed + */ + void XGBoosterFree( void* handle ); /*! * \brief set parameters * \param handle handle