[omniORB] operator new and bad_alloc (omniORB 2.8.0 pre-release 2)

Todd Fleming todd@aedar.com
Thu, 9 Sep 1999 15:02:41 -0400


I spotted code similar to the following while looking over files generated
by omniidl2:

X *p = new X(a, b, c);
if (!p) {
    throw CORBA::NO_MEMORY(0,CORBA::COMPLETED_NO);
}

I then searched through omniORB's sources and found similar constructs, but
no references to bad_alloc. This is correct for old versions of C++, but
does not work with the current standard. ISO/IEC 14882 18.4.1.1p3 states
that new must return non-null or throw std::bad_alloc.

I am considering replacing all instances of new in my copy of the sources
with new(nothrow), which should fix the problem. Has anyone tried this with
omniORB? The above example would be transformed to this:

X *p = new(nothrow) X(a, b, c);
if (!p) {
    throw CORBA::NO_MEMORY(0,CORBA::COMPLETED_NO);
}

Another option, to maintain compatibility with compilers whose operator
new() returns NULL, is replace all instances of new with something like
OMNI_NEW and define it like this:

#ifdef NEW_THROWS_EXCEPTION    // Yet another configuration macro...
    #define OMNI_NEW new(nothrow)
#else
    #define OMNI_NEW new
#endif

The example would then become:

X *p = OMNI_NEW X(a, b, c);
if (!p) {
    throw CORBA::NO_MEMORY(0,CORBA::COMPLETED_NO);
}

Todd Fleming
AEDAR Corporation