Tuesday, December 7, 2010

Program disappears when accessing 0 element in a empty vector

For some reason, some codes like below exist in our product:

   1: try
   2: {
   3:   std::vector<int> v;
   4:   int i = v[0];
   5:   int* pTemp = NULL;
   6:   *pTemp = 0;
   7: }
   8: catch(...)
   9: {
  10:   AfxMessageBox(L"haha");
  11: }
  12:  
Under VS2005, the above code can show message, but under 2008, it just disappears silently. Checking system event log can find some exception with code 0xc0000417. But the exception could not be caught, since it is the crt's invalid_parameter_info handler, internally, _invoke_watson is called and it unregister all existing unhandled exception handlers, then it terminates the process. (can be found under crt's code: "invarg.c). So, we have no change to catch this exception.


If wishing to catch such error, we can set:



   1: void myInvalidParameterHandler(const wchar_t* expression,
   2:    const wchar_t* function,
   3:    const wchar_t* file,
   4:    unsigned int line,
   5:    uintptr_t pReserved)
   6: {
   7:    throw 0;
   8: }
   9:  
  10:    _invalid_parameter_handler oldHandler, newHandler;
  11:    newHandler = myInvalidParameterHandler;
  12:    oldHandler = _set_invalid_parameter_handler(newHandler);
  13:  
some reference link can be found as below:

https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=101337&SiteID=210

No comments:

Post a Comment