If so, how to use ASSERT without having to worry about memory leak
issue? Thanks.
No memory leak here, the OS will clean up the process memory after assert()
has terminated it.
Still, I would suggest to save yourself from endless troubles and use
std::vector instead.
hth
Paavo
A failed assertion will terminate the process. During process termination
the heap is release for the o/s. So if you really want to assert, you should
not worry about memory leaks.
If your assert() macro/function does not terminate the process, what about
change the order of the two lines?
free(ptr);
assert(0==1);
Helge
An assertion failure will end the program right there, so leaking
memory is not an issue.
I would be more worried about other things that can be leaked, like
temporary files (which will, naturally, remain on the disk if not
removed by the program itself).
--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---
You've already got three good replies, the only thing I think missing
from those is if the "assert" function above is not the version from
cassert / assert.h, but rather something you've written that throws an
exception, then - if you have some reason to need it on the heap
rather than using std::vector<> as already suggested, you can use make
ptr a smart pointer type so it will automatically deallocate heap
memory (e.g. the Standard includes the quirky std::auto_ptr<>, or see
the freely downloadable Boost library for more safely and generally
usable versions, or the book Modern C++ Design by Alexandrescu for
some pretty comprehensive tips on writing your own, or his Loki
library for downloadable source code).
Cheers,
Tony