-1

I have a custom memory allocator written something like

void* Object::operator new(std::size_t size, const std::nothrow_t& nothrow_value)
{
  void *p = Allocator->malloc(size);
  return p;
}

Since the standard says I shouldn't throw an exception, I do not check whether allocation has been successful or not. Now I'm mocking Allocator object so that malloc function call returns me NULL. I'm using this operator something like this :

class TestClass: public Object
{
  public : TestClass()
  {
  }
}
testObject = new (std::nothrow)TestClass();

It is crashing here and bt of gdb shows something like this.. This pointer suddenly goes 0x0. can anybody expalin me..! If this is the case how can i handle this situation in my code.

#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
302Found
  • 490
  • 7
  • 19
  • How is `Allocator` declared and initialized? – πάντα ῥεῖ Sep 13 '13 at 18:18
  • It is passed as a constructor parameter, which is handled by another constructor(I have not mentioned it, thinking it was not that important) something like. TestClass (Allocator) : Object(Allocator){} and I would use it like testObject=new(std::nothrow)TestClas(MyAllocator); – 302Found Sep 13 '13 at 18:22
  • So `Allocator` is a member variable? That's not going to work. `operator new` has to be a static member function. Also in the code you post, `testObject` is not a pointer, but you initialize it with a pointer value. That also will not work. I think you need to show more code, preferably a (minimal) complete program that shows the problem. – bames53 Sep 13 '13 at 18:29
  • @bames53 : Allocator is passed to me as a constructor..(which is a mock in my testcase). Object class uses Allocator to allocate memory using "Allocator's" methods. OfCourse testObject is pointer.. which I thought is more "implying" that a new operator returns a pointer. Also,I typed out this code(';' missing at the end of the class definition.) so that I have shown enough for debugging. Anyways thanks for concern, I'll make sure I'll show up more code next time..:) – 302Found Sep 13 '13 at 18:47
  • You must not have an allocator as a member variable, because member `operator new` must be a static member and therefore cannot access member variables. Here's an example: http://ideone.com/ctMboI – bames53 Sep 13 '13 at 19:04

1 Answers1

3

Try adding an exception specification to the function definition, to tell the compiler that this operator new won't throw:

void* Object::operator new( size_t size, std::nothrow_t ) throw();

or if you have C++1:

void* Object::operator new( size_t size, std::nothrow_t) noexcept;

Without the exception specification, the compiler assumes that the operator new function will never return a null pointer.

James Kanze
  • 150,581
  • 18
  • 184
  • 329