How to work with COM classes and std::map
Hi,
I've this situation:
---------------------------------------------------------------------------
/*
* CH 1 -> [KEY1][KEY2]...[KEYn]
* CH 2 -> [KEY1][KEY2]...[KEYn]
* / \
* CComPtr<IWaveStream>
*/
typedef std::map < std::string, CComPtr<IWaveStream> > t_streams_map;
typedef std::map < short, t_streams_map >
t_ch_streams_map;
STDMETHODIMP CAudioPlayer::CreateStream(BSTR key,
short channel,
IWaveStream** ret_stream)
{
std::string sKey((char*)_bstr_t(key));
CComPtr<IWaveStream> iStream;
HRESULT hr = CComCoClass<CWaveStream>::CreateInstance(&iStream);
if (SUCCEEDED(hr))
{
//Store the object into the map
streams_map_[channel][sKey] = iStream;
if (ret_stream)
iStream->QueryInterface(IID_IWaveStream, reinterpret_cast<void**>
(ret_stream));
}
return rv;
};
//-----------------------------------------------------------------------
STDMETHODIMP CAudioPlayer::GetWaveStreamObj(BSTR key, short channel,
IWaveStream** pVal)
{
if (pVal)
{
//Find the object in the map
t_streams_map::iterator it = streams_map_[channel].find
( std::string((char*)_bstr_t(key)) );
//If I found it, I return it.
if ( it != streams_map_[channel].end() )
return it->second->QueryInterface(IID_IWaveStream,
reinterpret_cast<void**>(pVal));
return S_OK;
}
else
return E_POINTER;
};
//-----------------------------------------------------------------------
void CAudioPlayer::FinalRelease()
{
for (t_ch_streams_map::iterator map_it = streams_map_.begin();
map_it != streams_map_.end();
map_it++)
{
for (t_streams_map::iterator item = (*map_it).second.begin();
item != (*map_it).second.end();
item++)
(*item).second.Release();
}
};
---------------------------------------------------------------------------
In the CWaveStream class I have a member *m_stream (that is a _simple_
pointer to another class). This member is correctly initialized in the
constructor.
When I call CAudioPlayer::GetWaveStreamObj from the client, I get the
object but its *m_stream member is became 0!
Also, when I terminate the application the CAudioPlayer::FinalRelease
() is called but CWaveStream::FinalRelease() is not!
Someone see errors in my code?
Thanks!
Daniele.