shared_ptr<void> destructor called on nullptr

Viewed 73

I have a simple piece of code to load a shared library

  std::shared_ptr<void> hnld;
  hnld.reset( dlopen("libFoo.so", RTLD_NOW),dlclose);
  if(!hnld){
    std::cout << "Failed to load. "<< std::endl;
  }

When the library is loaded everything works right, but if it fails the message is displayed, however the code crashes when calling the destructor dlclose, which is expected as the object is invalid. As !hnld is false, shouldn't know the smart pointer that the destructor shouldn't be called ?

Note I know how to solve this by using another destructor who check if the object is nullptr before calling dlclose. My question is: why does the shared pointer behave like this?

0 Answers
Related