简单的字符串比较如何导致 OleException?

How can a simple string comparison cause an OleException?

我有这段C++代码:

static CString timeout(_T("TIMEOUT"));
if(strError.Left(7).CompareNoCase(timeout) == 0) return TRUE;

在我的直接 windows 中,我看到 strError 的以下值:

? strError
L""
ATL::CSimpleStringT<wchar_t,1>: L""

我遇到以下异常(我正在调试故障转储):

Unhandled exception at 0x74B4A9F2 in CRASH.DMP: Microsoft C++ exception: 
COleException at memory location 0x01BFFD14. occurred

我的调用堆栈如下:

   KERNELBASE.dll!_RaiseException@16()  Unknown
   msvcr110.dll!_CxxThrowException(void * pExceptionObject, const _s__ThrowInfo * pThrowInfo) Line 152  C++
   mfc110u.dll!AfxThrowOleException(long)   Unknown
   mfc110u.dll!ATL::AtlThrowImpl(long)  Unknown
   mfc110u.dll!ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > >::CompareNoCase(wchar_t const *)  Unknown
>  <Application>.exe!<Own_Class>::<Own_Function>(const ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > & strError) Line 106 C++

由于我在检查空字符串的 .Left(7) 时发生崩溃,我立即认为这是问题所在。但是我已经意识到这段代码通常工作正常(我只是仔细检查了这一点),并且在再次查看时,我看到异常不是访问冲突或 nullpointerexception,而是与 Ole 相关的异常。与此同时,我了解到问题不在 strError.Left(7) 部分:它似乎是 CompareNoCase() 方法,出错了,但是如何呢?

谁能告诉我我的代码哪里出了问题?

strError而言,它的创建方式如下:

CString strError;
...
strError = <function>();

其中 <function>() 类似于:

CString <function>(){
...
  return _T("fix string"); // or:
  return <Complicated_function_handling>();

在这种特殊情况下,我不知道 strError 是如何创建的(我知道它是空的,正如在 window 中清楚看到的那样,但我只有故障转储,所以我可以读取当前值,但我不知道它们的历史)。

一件事,这可能会有所帮助:我已经询问了 strError 的内存地址,并且我查看了内存中的数据(使用 Visual Studio 内存调试 window),我得到以下数据:

5c 18 9c 71 38 d9 ca 00 03 00 00 00 cc 3d c2 00 70 fe bf 01 94 13 b3 00 39 00 00 00 80 fe bf 01 be 58 aa 00 b0 3d c2 00 10 0c cd 00 44 03 00 00 01 00 00 00 d3 5f ac 00 8a 40 1d 72 8e f2 d6 73

提前致谢

通过 MFC 的源代码工作,抛出异常,因为 timeOut 是 NULL 字符串。发生这种情况的常见方式是尚未构建或已被破坏。 COleException 中存储的错误代码是 E_FAIL.

调用顺序是这样的:

  • CompareNoCase 调用 AtlIsValidString(如果 timeOut 在转换为 const char * 时为 NULL,则 return 为假)。
  • 由于 AtlIsValidString return 为假,与 ATLENSURE 宏关联的代码调用 AtlThrow(E_FAIL)AtlThrow 是定义为 ATL::AtlThrowImpl.
  • AtlThrowImpl 调用 AfxThrowOleException 因为错误代码不是 E_OUTOFMEMORY.