diff --git a/include/pybind11/internal/kernel.h b/include/pybind11/internal/kernel.h index 3b240f93..ddabe830 100644 --- a/include/pybind11/internal/kernel.h +++ b/include/pybind11/internal/kernel.h @@ -18,6 +18,20 @@ namespace pkbind { class handle; +struct action { + using function = void (*)(); + inline static std::vector starts; + + static void initialize() noexcept { + for(auto func: starts) { + func(); + } + } + + // register a function to be called at the start of the vm. + static void register_start(function func) { starts.push_back(func); } +}; + /// hold the object long time. struct object_pool { inline static int cache = -1; @@ -43,6 +57,7 @@ struct object_pool { /// alloc an object from pool, note that the object is uninitialized. static object_ref alloc() { + if(!indices_) { initialize(1024); } auto& indices = *indices_; if(cache != -1) { auto index = cache; @@ -91,20 +106,6 @@ struct object_pool { } }; -struct action { - using function = void (*)(); - inline static std::vector starts; - - static void initialize() noexcept { - for(auto func: starts) { - func(); - } - } - - // register a function to be called at the start of the vm. - static void register_start(function func) { starts.push_back(func); } -}; - template class lazy { public: diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 5bb56ef4..ae3ead89 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -15,10 +15,6 @@ inline bool initialized = false; /// initialize the vm. inline void initialize(int object_pool_size = 1024) { if(!initialized) { py_initialize(); } - - // initialize object pool. - object_pool::initialize(object_pool_size); - action::initialize(); initialized = true; } @@ -27,10 +23,10 @@ inline void initialize(int object_pool_size = 1024) { inline void finalize(bool test = false) { if(!initialized) { return; } object_pool::finalize(); + type::m_type_map.clear(); + capsule::tp_capsule.reset(); + cpp_function::tp_function_record.reset(); if(test) { - type::m_type_map.clear(); - capsule::tp_capsule.reset(); - cpp_function::tp_function_record.reset(); py_resetvm(); } else { py_finalize();