【问题标题】:VariantClear() throws an exception when called on A VARIANT containing a SAFEARRAY当在包含 SAFEARRAY 的 VARIANT 上调用时,VariantClear() 会引发异常
【发布时间】:2010-07-17 04:54:43
【问题描述】:

我正在尝试将 BYTES 数组中的一些数据包装到一个 VARIANT 中,但我似乎无法释放数据:

当我运行这段代码时...

SAFEARRAY * NewSArray;

SAFEARRAYBOUND aDim[1]; // a one dimensional array
aDim[0].lLbound = 0; //Sets the index to start from 0

//Sets the number of elements (bytes) that will go into the SAFEARRAY
aDim[0].cElements = pBuffer->GetSize();

NewSArray = SafeArrayCreate(VT_UI1, 1, aDim); // create a 1D SafeArray of BYTES

//Put the data from the man view into the SAFEARRAY
NewSArray->pvData = pBuffer->GetBuffer();

//FP Spread expects the spreadsheet data in the form of a VARIANT so we must pack the data from the SAFEARRAY into a
//VARIANT
VARIANT SpreadsheetBuffer;
VariantInit(&SpreadsheetBuffer);

SpreadsheetBuffer.vt= VT_ARRAY | VT_UI1; // set type to an array of bytes
SpreadsheetBuffer.parray= NewSArray;

try
{
    VariantClear(&SpreadsheetBuffer);
}
catch (char *str)
{
    AfxMessageBox(str);
}

我收到这条消息: “在 ... 0xC015000F 中的未处理异常:被停用的激活上下文不是最近激活的。”

顺便说一句,这条消息不会在我的 AfxMessageBox 中弹出。它似乎与变体类型有关,因为如果我不设置它,我不会得到异常。 pBuffer 中的数据只是之前从 SAFEARRAY 中拉出的 BYTE 数组。

有人知道我做错了什么吗?

谢谢

【问题讨论】:

  • 异常与你的代码sn-p完全无关。使调试器在未处理的异常上停止,并在您的问题中发布调用堆栈窗口内容。确保您已启用 Microsoft 符号服务器。

标签: c++ visual-c++ memory-leaks msdn variant


【解决方案1】:

SafeArrayCreate 创建一个安全数组并为pvData 成员分配内存。在此之后,您不应重置 pvData 成员。您应该将数据从pBuffer 复制到pvData 指向的内容中,或者使用SafeArrayAccessDataSafeArrayPutElement 函数。

【讨论】:

    猜你喜欢
    • 2020-06-10
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    相关资源
    最近更新 更多