If the new operator fails to allocate memory, the exception std::bad_alloc is only getting caught if I put a try-catch block immediately surrounding the new statement. If I instead have the try-catch block in a caller a few stackframes down below, it is not getting caught there and I'm getting an abnormal program termaination. Why does this happen? This is on Microsoft Visual Studio 2008.
Edit: Ok, here is the code that is not working. The function immediately below is where I'm calling the new, and the ones below are the stackframes below it. The last function is where I have the catch clause but it doesn't get caught there.
void HTTPResponseBuff::grow()
{
if (m_nMaxSize > m_nStartConstGrowSize)
m_nMaxSize += m_nConstGrowSize;
else
m_nMaxSize = 2 * m_nMaxSize;
char* pTmp = new char[m_nMaxSize];
. . .
}
void HTTPResponseBuff::write(const char* pBuf, size_t len)
{
char* pCh;
while (getRemainingCapacity(pCh) < len)
grow();
. . .
}
size_t HTTPTransport::responseCallback(void *pRespData, size_t size,
size_t nmemb, void *pRespBuff)
{
const char* pChar = (const char*)pRespData;
register int respDataLen = size * nmemb;
((HTTPResponseBuff*)pRespBuff)->write(pChar, respDataLen);
return respDataLen;
}
A few curl library stackframes here. These are C code, not C++.
ISTATUS HTTPTransport::invoke()
{
invokeCleanup();
//make the HTTP call
CURLcode retCode;
try{
retCode = curl_easy_perform(m_pCurl);
}
catch(std::bad_alloc& ba)
{
strcpy(m_pErrMsg,ba.what());
m_status = IFAILURE;
}
}
Also the stackframes at the time I caught the bad_alloc (in the catch clause immediately surrounding the new statement) is here: http://s289.photobucket.com/albums/ll211/spiderman2_photo_bucket/?action=view¤t=bad_alloc.jpg