-1

次のようなカスタムメモリアロケータを作成しました

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
4

1 に答える 1