简单的字符串比较如何导致OleException? [英] How can a simple string comparison cause an OleException?

查看:288
本文介绍了简单的字符串比较如何导致OleException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段C ++代码:

I have this piece of C++ code:

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

在我的即时窗口中,我看到strError的以下值:

In my immediate windows, I see following value for strError:

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

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

I'm getting following exception (I'm debugging a crash dump):

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()方法,出了错,但是怎么了?

As I was having a crash while checking .Left(7) of an empty string, I immediately assumed this was the course of the issue. However I've realised that this piece of code generally is working fine (I just double-checked this), and while having a second look, I see that the exception if not an access violation or a nullpointerexception, but an Ole related exception. In the meanwhile I've understood that the issue's not within the strError.Left(7) part: it seems to be the CompareNoCase() method, going wrong, but how?

任何人都可以启发我代码中的错误吗?

Can anybody enlighten me what's doing wrong in my code?

strError而言,它的创建过程如下:

As far as strError is concerned, it gets created as follows:

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

其中<function>()类似于:

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

在这种情况下,我不知道strError是如何创建的(我知道它是空的,正如在立即窗口中清楚看到的那样,但是我只有崩溃转储,所以我可以读取当前值,但是我不知道他们的历史).

In this particular case, I have no idea how strError has been created (I know it's empty, as clearly seen in the immediate window, but I only have the crash dump, so I can read current values, but I don't know their history).

可能有帮助的一件事:我询问了strError的内存地址,并且查看了内存中的数据(使用Visual Studio内存调试窗口),并且得到了以下数据: /p>

One thing, which might be helpful: I've asked for the memory address of strError, and I've looked at the data within memory (using Visual Studio Memory debug window), and I get following data:

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.

Working my thru the source for MFC, the exception is being thrown because timeOut is a NULL string. Common ways that this can happen are if it has not been constructed yet, or it has been destroyed. The error code stored within the COleException is E_FAIL.

呼叫顺序如下:

  • CompareNoCase调用AtlIsValidString(如果将timeOut转换为const char *时为NULL,则在您的情况下将返回false).
  • 由于AtlIsValidString返回false,因此与ATLENSURE宏关联的代码将调用AtlThrow(E_FAIL)(AtlThrow是定义为ATL::AtlThrowImpl的宏.
  • AtlThrowImpl调用AfxThrowOleException,因为错误代码不是E_OUTOFMEMORY.
  • CompareNoCase calls AtlIsValidString (which will return false in your case if timeOut, when converted to a const char *, is NULL).
  • Since AtlIsValidString returns false, the code associated with the ATLENSURE macro calls AtlThrow(E_FAIL) (AtlThrow is a macro that is defined as ATL::AtlThrowImpl.
  • AtlThrowImpl calls AfxThrowOleException since the error code is not E_OUTOFMEMORY.

这篇关于简单的字符串比较如何导致OleException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆