问题描述
在析构函数中调用VariantClear()期间,我在COleVariant析构函数中遇到间歇性的访问冲突。
I'm facing an intermittent Access Violation in COleVariant destructor during the call to VariantClear() inside the destructor.
第一次机会异常转储在 oleaut32!VariantClear方法中显示访问冲突。
The first chance exception dump shows the access violation in oleaut32!VariantClear method.
我在64位Windows Server上遇到此问题2008 R2 Datacenter SP1。此外,此访问冲突是间歇性的,即,相同的输入数据有时会出现访问冲突,而b $ b有时会运行正常。
这是一个已知问题
64位Windows Server 2008 R2 Datacenter SP1 ?
Is this a known issue on 64-bit Windows Server 2008 R2 Datacenter SP1 ?
下面给出了示例代码片段:
Tha sample code snippet is given below:
COleVariant var;
COleVariant var;
NUMPARSE np; //其成员正确填充了一些值。
NUMPARSE np; //its members are properly populated with some values.
字节数字[SOMELEN]; //有一些字节作为数字。
BYTE digits[SOMELEN]; //has some bytes as digits.
VarNumFromParseNum(& np,digits,VTBIT_R8,(LPVARIANT)var);
VarNumFromParseNum( &np, digits, VTBIT_R8, (LPVARIANT)var );
return; //在调用
COleVariant析构函数时发生访问冲突。
return; //access violation occurs here during the call toCOleVariant destructor.
谢谢,
Amit
推荐答案
NUMPARSE np; //其成员正确填充了一些值。
NUMPARSE np; //its members are properly populated with some values.
字节数字[SOMELEN]; //有一些字节作为数字。
BYTE digits[SOMELEN]; //has some bytes as digits.
VarNumFromParseNum(& np,digits,VTBIT_R8,(LPVARIANT)var);
VarNumFromParseNum( &np, digits, VTBIT_R8, (LPVARIANT)var );
return; //在调用
COleVariant析构函数时发生访问冲突。
return; //access violation occurs here during the call toCOleVariant destructor.
你是返回指向已封装在COleVariant类中的变体的指针。一旦函数返回,就会调用COleVariant上的析构函数释放底层变量,并使用悬空指针保留调用代码。
You are returning a pointer to a variant which has been encapsulated in a COleVariant class. As soon as the function returns, the destructor on COleVariant is called freeing the underlying variant and leaving the calling code with a dangling pointer.
通过编码轻松修复....
This is trivially fixed by coding....
VarNumFromParseNum( &np, digits, VTBIT_R8, var.Detach() );
这篇关于在析构函数内调用VariantClear()期间,COleVariant析构函数中的间歇访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!