破坏_variant_t会导致断点(C ++)

时间:2016-12-01 06:59:30

标签: c++ exception-handling com

我遇到麻烦terminating _variant_t types,它会触发断点,程序崩溃,

引起麻烦的部分代码如下:

double ConnectToHYSYS::GetExergy() {

    //In this method, I'm using early Binding, so no more Dispatchs, lets get the interfaces themselves

    int i;
    HRESULT hr = hyStream->QueryInterface(IID_PPV_ARGS(&hyBackDoor));

    if (SUCCEEDED(hr)) {
        cout << "Got the BackDoor safely" << endl;

        // Get Array of CorrelationNames but in type _variant_t
        _variant_t t = _variant_t("HysysCorrelation.300.[]:Name.0");
        InternalVariableWrapper = hyBackDoor->GetBackDoorTextVariable(&t);
        TextFlexVariable = InternalVariableWrapper->GetVariable();
        _variant_t CorrelationNames= TextFlexVariable->GetValues();



        //Conversion of _variant_t type to safe Array

        SAFEARRAY *psa;
        psa = CorrelationNames.parray;


        // Loop through safeArray of BSTR
            BSTR* pVals;
        HRESULT hr = SafeArrayAccessData( psa, (void**)&pVals ); // direct access to SA memory
        if( SUCCEEDED( hr ) )
        {
            long lowerBound, upperBound;  // get array bounds
            SafeArrayGetLBound( psa, 1, &lowerBound );
            SafeArrayGetUBound( psa, 1, &upperBound );

            long cnt_elements = upperBound - lowerBound + 1;
            for( i = 0; i < cnt_elements; ++i )  // iterate through returned values
            {
                BSTR lVal = pVals[ i ];

                //Convert to normal String for comparison with Mass Exergy
                string CorrelationFinal= ConvertBSTRToMBS( lVal );
                std::cout << "element " << i << ": value = " << CorrelationFinal << std::endl;

                if( CorrelationFinal == "Mass Exergy" ){ break; }
            }
            SafeArrayUnaccessData( psa );
        }
        SafeArrayDestroy( psa );
        if (SUCCEEDED(hr)) {
            cout << "Got the BackDoor Text Variable safely" << endl;
        }

        if (FAILED(hr)) {
            cout << "Couldnt get the BackDoor Text Variable" << endl;
        }
    }

    if (FAILED(hr)) {
        cout << "Couldnt get the BackDoor" << endl;
    }
// Get Exergy Moniker
    string str = to_string(i);
    string ExergyMoniker ="HysysCorrelation.300." + str + ":ExtraData.550.0";

    // OLE accepts only _variant_t type and we need char array for that conversion... converting string to char array

    char tab2[ 1024 ];
    strncpy_s( tab2, ExergyMoniker.c_str(), sizeof( tab2 ) );
    tab2[ sizeof( tab2 ) - 1 ] = 0;

    //Get the exergy itself

    _variant_t t = _variant_t( tab2 );
    InternalVariableWrapper = hyBackDoor->GetBackDoorVariable( &t );
    RealVariable = InternalVariableWrapper->GetVariable();
    _variant_t ExergyValue = RealVariable->GetValue( "kJ/kg" );
    double ExergyValueDouble = ExergyValue.dblVal;

    return ExergyValueDouble;

所以,任何想法为什么会导致这样的错误?当我点击“Break”时,它指向这个内联代码(comutil.h)

inline _variant_t::~_variant_t() throw()
{
    ::VariantClear(this);
}

此外,当我在调试时单击继续时,程序继续没有麻烦,这是否意味着我可以处理该异常?

1 个答案:

答案 0 :(得分:1)

您的代码中存在许多潜在问题,例如,您的所有方法GetBackDoorTextVariable(),GetValues(),GetBackDoorVariable(),GetValue()都可能构建错误的变体。

但是,有一点似乎没有问题,那就是你如何处理outer: for (int i : arr1) { for (int j : arr2) { if (i == j) { continue outer; } } System.out.println("Arr2 doesn't contain number: " + i); } 实例。

因为CorrelationNames是一个自动包装类并为你处理内存释放,所以你不应该过于深入地使用它。但是在您的代码中,您释放了此变体所拥有的内存(使用_variant_t调用),而不将SafeArrayDestroy成员设置为null。当析构函数运行时,它会尝试释放空指针并崩溃。

所以,删除parray行,它应该更好,至少在这个问题上。