![]() |
警告 |
---|---|
典型错误和误用位于本节。请仔细阅读,这将节省大量的调试时间! |
delete
或 free()
时崩溃。catch(...)
代码块中崩溃。try { auto f = dll::import<int()>(path_to_pugin, "function"); f(); // `f` goes out of scope } catch (const std::exception& e) { std::cerr << e.what(); }
f
超出作用域时,插件被卸载,对异常代码的引用被破坏。任何尝试使用异常变量都可能使用悬空引用,导致段错误。修复你的代码auto f = dll::import<int()>(path_to_pugin, "function"); try { f(); // `f` goes out of scope } catch (const std::exception& e) { std::cerr << e.what(); }
void foo() { shared_ptr<int> p; try { auto f = dll::import<shared_ptr<int>()>(path_to_pugin, "function"); p = f(); // `f` goes out of scope } catch (const std::exception& e) { std::cerr << e.what(); } std::cout << *p; // crashes here }
shared_ptr<int>
。它保留了一个类型擦除的删除器,该删除器的代码位于插件中。在 p
销毁时,shared_ptr<int>
尝试调用该删除器,但是插件已被卸载,删除器的代码不再可用。any
function
shared_ptr
std::type_index
std::type_info
std::exception_ptr
std::unique_ptr<Base>
持有来自插件的 Derived
类型