I have seen that in sipxtapi, sometimes we check for new failure with an
if, sometimes we don't.

Standard behaviour is that if it runs out of memory, it throws
std::bad_alloc. This holds for visual studio and gcc 2.95
(http://www.gnu.org/software/gcc/gcc-2.95/c++features.html - * operator
new now throws bad_alloc where appropriate.)
, but for WinCE, it returns 0.

Checking with an if after each allocation and using delete can produce
very complicated code.

I don't want to write this kind of code:

SIPX_LINE_DATA* pData = new SIPX_LINE_DATA();
if(pData == 0)
{
  return 0;
}
pData->lineURI = new Url(uri);
if(pData->lineURI == 0)
{
  delete pData;
  return 0;
}

pData->pMutex = new OsRWMutex(OsRWMutex::Q_FIFO);
if(pData->pMutex == 0)
{
  delete pData->lineURI;
  delete pData;
  return 0;
}

And if we had 10 more members, then .. oh god

It becomes even more complicated in long functions that have lots of
nested ifs and new's in them.

We could have a macro "AllocationCheck(P)" that would check whether the
pointer is NULL, and if it is, then print the line of code to cerr with
an error message and do exit(-somecode).

What do you think?

Jaroslav Libak
_______________________________________________
sipxtapi-dev mailing list
[email protected]
List Archive: http://list.sipfoundry.org/archive/sipxtapi-dev/

Reply via email to