我有一个自定义内存分配器,写了类似的东西
void* Object::operator new(std::size_t size, const std::nothrow_t& nothrow_value)
{
void *p = Allocator->malloc(size);
return p;
}
由于标准说我不应该抛出异常,所以我不检查分配是否成功。现在我正在模拟 Allocator 对象,以便 malloc 函数调用返回 NULL。我正在使用这个运算符,如下所示:
class TestClass: public Object
{
public : TestClass()
{
}
}
testObject = new (std::nothrow)TestClass();
它在这里崩溃,gdb 的 bt 显示类似这样的内容。这个指针突然变为 0x0。任何人都可以解释我..!如果是这种情况,我该如何在我的代码中处理这种情况。
#0 0x000000000040acd3 in TestClass::TestClass (this=0x0) at TestAllocatable.cpp:72
#1 0x00000000004074ed in TestAllocatableFixture_Positive2_Test::TestBody (this=0x67cdc0) at TestAllocatable.cpp:238
#2 0x0000000000443c98 in void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) ()
#3 0x000000000043eaf8 in void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) ()
#4 0x000000000042bab8 in testing::Test::Run (this=0x67cdc0) at ../gtest/src/gtest.cc:2162