简单的弦比较如何引起olexeception

How can a simple string comparison cause an OleException?

本文关键字:何引起 olexeception 比较 简单      更新时间:2023-10-16

我有此C 代码:

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

在我的直接窗口中,我看到了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的(我知道它是空的,正如在即时窗口中清楚地看到的,但是我只有崩溃转储,所以我可以读取当前值,但是我不喜欢知道他们的历史)。

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

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是一个空字符串,因此正在抛出例外。如果尚未构建这种情况,或者已经被破坏了。COleException中存储的错误代码是E_FAIL

呼叫序列是这样的:

  • CompareNoCase调用AtlIsValidString(如果timeOut转换为const char *,则在您的情况下返回false,为null)。
  • 由于AtlIsValidString返回false,因此与ATLENSURE宏连接的代码AtlThrow(E_FAIL)AtlThrow是一个宏,定义为ATL::AtlThrowImpl
  • AtlThrowImpl调用AfxThrowOleException,因为错误代码不是E_OUTOFMEMORY