Now, should pInt
be null in any of iteration, then this test case will stop and pList
will not be freed.
Assuming you didn't override the new
operator (if you did, you probably wouldn't be asking about this), and assuming your compiler is not buggy, pInt
will never be null. On failure, new
throws a std::bad_alloc
exception, it doesn't return null.
Moreover, assertions are for things that should always hold (as is the case), no matter what. An assertion failure is a bug. There's no point in adding code to clean up after an assertion failure: just fix the bug instead.
Now to the freeing... The sample code provided can just forget new
and use automatic objects:
List pList;
for (...)
{
Integer pInt = Integer();
pList.Add(pInt);
}