使用 CComVariant 访问 SafeArray 时出现问题

发布于 2024-07-15 02:52:37 字数 1657 浏览 6 评论 0原文

我有以下代码块

 /////////////////////////////////////
 CComVariant newVal;

 //pass the CComVariant and get the strings array!!!
 GetStrList(newVal);
 USES_CONVERSION;

 if (((newVal.vt & VT_ARRAY) == VT_ARRAY) && ((newVal.vt & VT_BSTR) == VT_BSTR))
 {
SAFEARRAY* paArray = newVal.parray;
BSTR * str = NULL;
SafeArrayAccessData(paArray, (void**)&str);

SafeArrayUnaccessData(paArray);

long lLBound = 0;
long lUBound = 0;
long nCount = 0;

if (FAILED(SafeArrayGetLBound(paArray, 1, &lLBound)) ||
    FAILED(SafeArrayGetUBound(paArray, 1, &lUBound)))
{
    ASSERT(false);
    return FALSE;
}


nCount = ( lUBound - lLBound + 1 );
for (int i = 0 ; i < nCount ; i++)
{           
    m_cstrList.AddString(W2T(str[i]));                  
}   
//SafeArrayDestroy(paArray); ---> is it required here???

 }

 /////////////////////////////////////

方法返回安全数组

 HRESULT GetStrList(VARIANT *pVal)
 {
USES_CONVERSION;

if (!pVal)
    return E_FAIL;

SAFEARRAYBOUND bound[1]; //single dimension array
bound[0].lLbound = 0;
bound[0].cElements = 10;

SAFEARRAY * A = SafeArrayCreate(VT_BSTR, 1, bound);

BSTR * str = NULL;
SafeArrayAccessData(A, (void**)&str);

//user wants the NT view OPC drivers list.
for (int i=0;i<10;i++)
{       
    str[i] = SysAllocString(T2W(mystrings[i]));     
}


VariantInit(pVal);
pVal->vt        = VT_ARRAY | VT_BSTR;
pVal->parray    = A;

SafeArrayUnaccessData(A);
A = NULL;

return S_OK;
 }

我的疑问是,上面的第一块代码有任何内存泄漏吗? CComVariant 本身是否处理有关清洁的所有事情? 或者我也手动执行SafeArrayDestroy(paArray);

提前致谢!

I have following block of code

 /////////////////////////////////////
 CComVariant newVal;

 //pass the CComVariant and get the strings array!!!
 GetStrList(newVal);
 USES_CONVERSION;

 if (((newVal.vt & VT_ARRAY) == VT_ARRAY) && ((newVal.vt & VT_BSTR) == VT_BSTR))
 {
SAFEARRAY* paArray = newVal.parray;
BSTR * str = NULL;
SafeArrayAccessData(paArray, (void**)&str);

SafeArrayUnaccessData(paArray);

long lLBound = 0;
long lUBound = 0;
long nCount = 0;

if (FAILED(SafeArrayGetLBound(paArray, 1, &lLBound)) ||
    FAILED(SafeArrayGetUBound(paArray, 1, &lUBound)))
{
    ASSERT(false);
    return FALSE;
}


nCount = ( lUBound - lLBound + 1 );
for (int i = 0 ; i < nCount ; i++)
{           
    m_cstrList.AddString(W2T(str[i]));                  
}   
//SafeArrayDestroy(paArray); ---> is it required here???

 }

 /////////////////////////////////////

method returing the safe arrays

 HRESULT GetStrList(VARIANT *pVal)
 {
USES_CONVERSION;

if (!pVal)
    return E_FAIL;

SAFEARRAYBOUND bound[1]; //single dimension array
bound[0].lLbound = 0;
bound[0].cElements = 10;

SAFEARRAY * A = SafeArrayCreate(VT_BSTR, 1, bound);

BSTR * str = NULL;
SafeArrayAccessData(A, (void**)&str);

//user wants the NT view OPC drivers list.
for (int i=0;i<10;i++)
{       
    str[i] = SysAllocString(T2W(mystrings[i]));     
}


VariantInit(pVal);
pVal->vt        = VT_ARRAY | VT_BSTR;
pVal->parray    = A;

SafeArrayUnaccessData(A);
A = NULL;

return S_OK;
 }

My doubt is, above first block of code has any memory leaks?
Does the CComVariant itself handle every thing about the cleaning?
or do i also manually do SafeArrayDestroy(paArray);

Thanks in Advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑看君怀她人 2024-07-22 02:52:37

CComVariant 析构函数调用 VariantClear() 来释放变体所封装的任何内容,包括数组。

需要注意的是:调用 VariantClear() 时不应锁定数组。 这意味着,如果在 SafeArrayAccessData() 之后但在 SafeArrayUnaccessData() 之前抛出异常,则不会调用后者,并且 VariantClear() 将不会释放资源。

因此,您最好编写一个括号类来配对 SafeArrayAccessData() 和 SafeArrayUnaccessData() 调用。

CComVariant destructor calls VariantClear() which frees whatever the variant was incapsulating, arrays included.

One caveat: the array should not be locked at the time when VariantClear() is called. This means that if an exception is thrown after SafeArrayAccessData() but before SafeArrayUnaccessData() the latter will not be called and VariantClear() will not free resources.

Therefore you better write a bracket class for pairing SafeArrayAccessData() and SafeArrayUnaccessData() calls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文