in C++, suppose there is a method in a Helper class which returns me a pointer to another object. I don't have any more information about this method except the method signature and the type of returned object. How to do memory management in this case?
I attempted to use smart pointer as this function below:
void f() {
auto_ptr<SomeClass> p_someClass = p_Helper->getSomeclass();
p_someClass->doSomething();
}
The memory allocated to p_someClass is deallocated as soon as f() goes out of scope. However, what if getSomeclass() doesn't allocate new memory but simply return a "singleton" pointer (and p_Helper is also singleton)? Then the next call to p_Helper->getSomeclass() will be in trouble.
What is the usual way to handle this issue, esp when there is very little documentation on the Helper class?