Why does SafeArrayUnaccessData throws debug assertion failed

194 views Asked by At

Below is my code

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;
}

Here the SafeArrayUnaccessData(sa) throws debug assertion failure .

When I comment out SafeArrayUnaccessData(sa) the program works fine

enter image description here

0

There are 0 answers