I would not use scoped_ptr
for that purpose, but rather unique_ptr
in C++11 and auto_ptr
in older compilers, both of which are equivalent for your particular use case. As of whether it is overkill, no, it is not, it is the only option to providing exception safety (say anything in the code of either myfunc
or callsomeotherfunc
throws, you want the memory to be released). Performance wise, the three options are equivalent to having a delete
call at the end of the function in the case where exceptions are not thrown, and faster than having a try/catch
block with a delete
that rethrows the exception in the event of exceptions.
Additionally, it seems that you are allocating from a factory, in some designs that factory will have a deallocate
operation that needs to be called, rather than delete
. If that is the case, you might consider a different smart pointer (shared_ptr
from the standard, which would be overkill if delete
is the proper way of releasing the memory; or some short of managed_ptr
for which you can also provide a deleter)
boost::scoped_ptr
to be as fast in the non-exceptional case and much faster in the exceptional one (throwing is costly with the modern ZeroCost model). – Tenpin