【发布时间】:2019-01-07 19:55:56
【问题描述】:
当分配器由于内存有限而失败时。该应用程序正在崩溃。抛出 bad_alloc 或返回 nullptr 不会停止崩溃。有人知道吗?
pointer allocator<T>(size_type count) const
{
void* buffer = new (count * sizeof(T));
if (!buffer) // if buffer == nullptr crashes app
throw bad_alloc; // doing this crashes app
/* or alternatively
* try {
* void* buffer = new (count * sizeof(T));
* } catch (const std::exception& e) {
* std::cerr << "failed to allocate " << count << std::endl;
* return nullptr;
* }
*/
}
那么如何优雅地关闭应用并说内存不足?
【问题讨论】:
-
什么样的崩溃?什么是回溯?
-
@n.m.目前还不清楚这里要问什么,但我怀疑这个问题是重复的。
-
这里是示例jrruethe.github.io/blog/2015/11/22/allocators。尝试在分配器中将 UINT64_MAX 分配为
count看看会发生什么