Friday, December 2, 2011

new nothrow

I don't use STL, and I don't C++ exceptions.  I avoid them for a few reasons: under debugger, they make it a pain to find and walk the faulting call stack after they are thrown, and mostly I lean towards a more C style of programming style personally.  Still, you need to use the new operator to create new C++ objects, and new can throw and exception in low resource conditions.  There are a few ways to change this default behavior so that it just returns NULL instead of throwing.

One way is to add this into your sources file:

TARGETLIBS=\    
    $(SDK_LIB_PATH)\nothrownew.obj       \

Another way is to add a no throw each time you use the new operator:

    ClassX *pObj = new (std::nothrow) ClassX();

    if (!pObj)
        return E_OUTOFMEMORY;

No comments:

Post a Comment