I'm not familiar with smart pointers, but I take it the pointer frees the memory before it is removed.
You should get familiar with them. RAII should always (barring some extreme circumstances) be used as it provides the best defense against leaks and other issues caused by mismatching initialization/deinitialization.
unique_ptr, specifically, will delete the object in its destructor. Ensuring that as soon as the unique_ptr goes out of scope (ie: removed from the vector), the pointed to object will be deleted.
I was thinking more along the lines of
delete intPointers.at(n);
But I'm uncertain if this actually works.
Yes that works but is not ideal.
You'll have to make sure that you delete every element upon removal. This includes the "emptying" of the vector when the vector is destructed.
If you are manually deleting things, you will have to go through an delete all entries before the vector goes out of scope, or else everything contained in the vector will leak.
void someFunc()
{
std::vector<int*> foo;
// ... add a bunch of new'd stuff to foo here ...
} // <- MEMORY LEAK
// to correct... you must delete everything in the vector manually before it goes out of
// scope
void someFunc()
{
std::vector<int*> foo;
// ... add a bunch of new'd stuff to foo here ...
while( !foo.empty() )
{
delete foo.back();
foo.pop_back();
}
} // <- no more leak
// but even that doesn't necessarily protect you all the time....
void someFunc()
{
std::vector<int*> foo;
// ... add a bunch of new'd stuff to foo here ...
if( something )
return; // MEMORY LEAK
someRoutineThatThrowsAnException(); // MEMORY LEAK
while( !foo.empty() )
{
delete foo.back();
foo.pop_back();
}
}
So yeah. avoid manual deletions. Use smart pointers.