...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
Warning | |
---|---|
Typical errors and missuses are located in this section. Please read carefully, this will save a lot of debugging time! |
delete
or free()
.
catch(...)
block.
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
goes out of scope, the plugin gets
unloaded and the reference to the exception code is broken. Any attempt
to use the exception variable may use the dangling reference, leading to
a segmentation fault. Fix your code:
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>
.
It keeps a type erased deleter, code for that deleter is located in plugin.
On destruction of p
, shared_ptr<int>
attempts to call that deleter, however the plugin was already unloaded
and the code for deleter is not available any more.
any
function
shared_ptr
std::type_index
std::type_info
std::exception_ptr
std::unique_ptr<Base>
holding a Derived
type from plugin