_bstr_t is crashing on me

I am using a COM function that returns a "_bstr_t" object.


{
_bstr_t versionStr;
versionStr = m_spaPtr->GetVersion();
}


When "versionStr" goes out of scope the program crashes with an Access Violation. What am I not understanding here? How can I resolve this problem?


This is what I see in the .TLI file


inline _bstr_t IEaX::GetVersion ( ) {
BSTR _result;
HRESULT _hr = get_Version(&_result);
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _bstr_t(_result, false);
}


Throwing out a wild guess:
Does this have anything to do with different heap managers between the DLL and the application? DLL is allocating memory and application is trying to free that memory? I know that _bstr_t allocates memory for its internal buffer. But I'm wondering if this is the source of the problem or if something else is wrong.
[980 byte] By [Rigel] at [2007-11-19 22:46:42]
# 1 Re: _bstr_t is crashing on me
use exception handling

try
{

_bstr_t versionStr;
versionStr = m_spaPtr->GetVersion();

}catch(_com_error *err)
{

}

i'am not sure whether i have addressed your problem.
pradish at 2007-11-10 23:33:04 >
# 2 Re: _bstr_t is crashing on me
Throwing out a wild guess:
Does this have anything to do with different heap managers between the DLL and the application? DLL is allocating memory and application is trying to free that memory? I know that _bstr_t allocates memory for its internal buffer. But I'm wondering if this is the source of the problem or if something else is wrong.

That sounds likely to me as the constructor for _bstr_t looks like this for the overload you are using:

inline _bstr_t::_bstr_t(BSTR bstr, bool fCopy)
: m_Data(new Data_t(bstr, fCopy))
{
if (m_Data == NULL) {
_com_issue_error(E_OUTOFMEMORY);
}
}

So if the _bstr_t gets constructed in the dlls heap manager and freed using the applications heap manager it sounds likely that this could give the problem you describe.

Now how to solve it? Depends on your situation.

1. Refactor away _bstr_t (might require large changes to your interfaces)
2. Make sure the heap-manager is shared across the dlls and applications. There probably is a way to accomplish this but I'm unsure exactly how to progress as I never had the heap-manager problem myself. Could try dynamically link against the CRT dll in all dlls and application but that's just a wild guess.

Hope this helps
marten_range at 2007-11-10 23:34:04 >
# 3 Re: _bstr_t is crashing on me
Simply use _tcsdup in your Program and Hopefully it will solve your problem for more details have a look in MSDN.
and also have a look on this link .it's a very good link.

http://www.codeproject.com/buglist/bug_in__bstr_t_amp__varia.asp?df=100&forumid=3473&exp=0&select=1076955

Thanx...
humptydumpty at 2007-11-10 23:35:07 >
# 4 Re: _bstr_t is crashing on me
pradish, marten_range, humpty - Thanks!! I'll check out your suggestions and get back if I have further problems. Appreciate your help! :thumb:
Rigel at 2007-11-10 23:36:07 >