Re: How to work with COM classes and std::map
On 13 Ott, 14:03, "Igor Tandetnik" <itandet...@mvps.org> wrote:
Barzo wrote:
Ok, I post the entire code.
The code you show never calls SetStreamObj, so as far as I can tell, stea=
m_ would always be NULL. I don't see any place where an instance of EuroAud=
ioLib::IWaveStream is created.
Hi Igor,
I changed the CreateStream method and now it works:
STDMETHODIMP CAudioPlayer::CreateStream(BSTR key,
short channel,
IWaveStream** ret_stream)
{
std::string sKey((char*)_bstr_t(key));
CComPtr<IWaveStream> iStream;
CWaveStream* pStream;
HRESULT hr = CComCoClass<CWaveStream>::CreateInstance(&iStream);
if (SUCCEEDED(hr))
{
//Store the object into the map
streams_map_[channel][sKey] = iStream;
pStream = (CWaveStream*)iStream.p;
pStream->SetStreamObj( device->CreateStream(sKey, channel) );
if (ret_stream)
iStream->QueryInterface(IID_IWaveStream, reinterpret_cast<void**>
(ret_stream));
}
return rv;
};
void WaveStreamEvents::OnStreamDebugMsg(EuroAudioLib::IWaveStream*
Sender, std::string msg)
{
_bstr_t b_msg(msg.c_str());
WaveStreamObj_.Fire_OnStreamDebugMsg( &WaveStreamObj_, b_msg.copy
() );
I'm pretty sure you are leaking a BSTR here.
Maybe I have to use .Detach():
WaveStreamObj_.Fire_OnStreamDebugMsg(&WaveStreamObj_, b_msg.Detach())
STDMETHODIMP CWaveStream::get_TxIP(BSTR* pVal)
{
if (stream_) {
_bstr_t* sIP = new _bstr_t( stream_->getTxIP().c_str() );
*pVal = sIP->copy();
And here you are leaking both a heap-allocated _bstr_t object and the BST=
R it wraps.
Is this better?
if (pVal) {
_bstr_t* bstr = new _bstr_t( device_->getFileName(channel).c_str
() );
*pVal = bstr->Detach();
delete bstr;
}
Thanks a lot for your suggestions!
Daniele.