为什么SafeArrayUnaccessData抛出调试断言失败

时间:2015-06-12 07:09:45

标签: c++ safearray

以下是我的代码

int _tmain(int argc, _TCHAR* argv[])
{
    CComSafeArray<long> sa;
    vector<long> vec;
    for (int i = 0; i<3 ; i++   )
    {
        func(&(sa.m_psa));
        long* pVals;

        HRESULT hr = SafeArrayAccessData(sa, (void**)&pVals);
        if (SUCCEEDED(hr))
        {
            long lowerBound, upperBound;  // get array bounds
            SafeArrayGetLBound(sa, 1 , &lowerBound);
            SafeArrayGetUBound(sa, 1, &upperBound);

            long cnt_elements = upperBound - lowerBound + 1; 
            for (int i = 0; i < cnt_elements; ++i)  // iterate through returned values
            {                              
                 vec.push_back(pVals[i]);   
            }       
        }

    // When no longer need "raw".
        SafeArrayUnaccessData(sa); // tried sa.m_psa too
    }
    return 0;
}

这里SafeArrayUnaccessData(sa)抛出调试断言失败。

当我注释掉SafeArrayUnaccessData(sa)时,程序运行正常

enter image description here

0 个答案:

没有答案