When running the following code, I see the memory consumption of the process grow. Is there a memory leak in my code, is there a memory leak in the std implementation, or is it intended behaviour? It's running on a Windows 10 machine; both Visual Studio and the task manager show approximately 1MB memory growth per minute.
for (int i = 0; i < 10000; i++) {
std::exception_ptr exptr = nullptr;
std::string errmsg = "some error";
try {
throw std::runtime_error(errmsg.c_str());
}
catch (...) {
exptr = std::current_exception();
}
if (exptr) {
try {
rethrow_exception(exptr);
}
catch (std::exception const& ex) {
exptr = nullptr;
std::cout << ex.what() << std::endl;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(10ms));
}
When directly throwing and logging without delay (without using std::exception_ptr
), the memory consumption doesn't grow.
std::exception_ptr
is advertised as behaving like a smart pointer, so when resetting it (setting it to nullptr
), the underlying resource should be destroyed. Therefore, I expect the underlying exception to be freed appropriately.