diff --git a/util/RTCContext.h b/util/RTCContext.h index 51b6689..c5a9930 100644 --- a/util/RTCContext.h +++ b/util/RTCContext.h @@ -57,14 +57,22 @@ public: return instance; } static void** numpy_api() { - void** ptr = reinterpret_cast(RTC_PLUGINS_ARRAY_API); + static void** api = [](){ + // 强制初始化NumPy + if (_import_array() < 0) { + PyErr_Print(); + throw std::runtime_error("NumPy initialization failed"); + } - // 深度验证API有效性 - if (!ptr || !ptr[93]) { // 93是PyArray_SimpleNew的偏移量 - PyErr_Print(); - throw std::runtime_error("Invalid NumPy API structure"); - } - return ptr; + void** ptr = reinterpret_cast(RTC_PLUGINS_ARRAY_API); + if (!ptr || !ptr[93]) { + std::cerr << "NumPy API corrupt! Expected at 93: " + << (ptr ? (void*)ptr[93] : nullptr) << std::endl; + abort(); + } + return ptr; + }(); + return api; } RTCContext(const RTCContext&) = delete; RTCContext& operator=(const RTCContext&) = delete;