It is not a leak. Generally speaking, a leak is when resources are allocated by your program and you lose all handles to them. An even stricter definition of a leak is when this resource allocation happens repeatedly over time.
A simple call to clear
does not fulfill that definition, because you still have access to the std::vector
object after the function call; the object does not simply disappear. You can still call shrink_to_fit
on it, or swap
it with an empty vector. And even that should not be necessary, because eventually, the std::vector
will be destructed, and the destructor deallocates the occupied memory, or more correctly speaking, it deallocates the storage, because what happens on the OS or even hardware level is not in the scope of C++ language rules.
If the destructor is not run because the std::vector
is never destroyed due to some buggy dynamic memory handling on your part, then the existence of the std::vector
object itself already causes a potential leak. The problem is then not with the storage handled by the std::vector
.
Note that clear
still causes the std::vector
's elements to be destroyed immediately. This has the important effect that their destructors are run. So when you have, for example, a std::vector<std::string>
, and you call clear
on it, then all the individual std::string
destructors are run, and of course that causes storage to be deallocated immediately, provided that there was actually some dynamic content handled by the strings (i.e. Small-String Optimisation was not used). But it's not the storage handled by std::vector
, but the storage handled by the std::string
s'.