gpt4 book ai didi

c++ - 破坏 _variant_t 导致断点 (C++)

转载 作者:行者123 更新时间:2023-11-30 05:19:58 26 4
gpt4 key购买 nike

我在终止 _variant_t 类型时遇到麻烦,它触发断点并且程序崩溃,

引起问题的部分代码如下:

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;

那么,知道为什么会导致这样的错误吗?当我单击“中断”时,它指向此内联代码 (comutil.h)

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

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

最佳答案

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

但是,有一点似乎不太正常,那就是您如何处理 CorrelationNames 实例。

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

因此,删除 SafeArrayDestroy(psa) 行,它应该可以更好地工作,至少对于这个问题。

关于c++ - 破坏 _variant_t 导致断点 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40904388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com