如果未事先调用variantInit,变体将崩溃软件崩溃?
我有一个例外,我不能轻易复制,但是我非常怀疑它发生在variantClear()
中。
我的函数可以定义一个变体,然后将其传递给另一个变体,而无需首先调用variantinit()
。然后调用函数在此变体上调用variantClear()
,这可能是例外的来源。
void Func1()
{
VARIANT vData;
//VariantInit(&vData); // no variant clear was done. Will adding this line stop the crash below?
Func2(vData);
}
void Func2(VARIANT& vData)
{
// some code here
VariantClear(&vData); <-- this line crashes, why??
// some code here
}
谁能解释为什么variantClear()
可能会引发异常?在func1()
中呼叫variantinit()
停止此异常发生吗?
I have an exception that I cannot easily replicate, but I have a very strong suspicion that it happens during VariantClear()
.
I have a function that defines a variant and then passes it off to another variant without calling VariantInit()
on it first. The called function then calls VariantClear()
on this variant, which is the likely source of the exception.
void Func1()
{
VARIANT vData;
//VariantInit(&vData); // no variant clear was done. Will adding this line stop the crash below?
Func2(vData);
}
void Func2(VARIANT& vData)
{
// some code here
VariantClear(&vData); <-- this line crashes, why??
// some code here
}
Can anyone explain why VariantClear()
could be throwing an exception? Will calling VariantInit()
in Func1()
stop this exception from happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

VariantClear
检查VARIANT
的前 16 位以查找变体类型。根据类型的不同,
VariantClear
可能会调用CoTaskMemFree
或将变体视为其调用Release
的 COM 指针。如果类型无效,您可能会崩溃或释放一些不相关的内存。如果类型是 VT_EMPTY、VT_NULL 或数字类型,它只会将所有字段设置为零。
如果您不初始化
VARIANT
,则类型未定义,它可能是上一操作留在内存中的任何值。因此,您必须首先在VARIANT
上调用VariantInit
。VariantClear
checks the first 16 bits of theVARIANT
to find the variant type.Depending on the type,
VariantClear
might callCoTaskMemFree
or treat the variant as a COM pointer it callsRelease
on. If the type is invalid you might crash or free some unrelated memory.If the type is
VT_EMPTY
,VT_NULL
or a number type it just sets all fields to zero.If you don't initialize the
VARIANT
, the type is undefined, it could be any value left in memory from a previous operation. Therefore you must callVariantInit
first on theVARIANT
.