'When is safe wrapping legacy raw pointers from factories with smart pointers?

When is safe to wrap in smart pointers (unique_ptr or shared_ptr) the raw pointers returned by factories of C++ frameworks started to develop before c++11?

For example when everything happen within the same function:

int main(int argc,char** argv)
 {
  //.....
  auto* runManager = G4RunManagerFactory::CreateRunManager(G4RunManagerType::Default);
  //... many lines later
  delete runManager;
 }

It believe that it is harmless to put the returned raw pointer in a unique_ptr and take out the delete at the end of the function.

But besides this specific case, I am not sure if there are other cases in which this can be done safely - and your code gain in readability / maintainability is worth the effort.

In particular I am using a C++ framework toolkit that developed its own way of tracking and disposing the objects created by the framework, so I should have to think twice (or more) before using the returned pointers in a way not originally intended.

Any suggestion is very welcome.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source