Problem with a collection of event sinks
I have created an object that contains a collection of inner objects, the
main object needs to respond to the events of these inner objects within the
collection.
I have created a template class based on IDispEventImpl, which sets up the
event sink for the inner objects within the collection and forwards the calls
to the main object.
template <class T>
class CObjEventSink : public IDispEventImpl<0, CObjEventSink,
&DIID__IEventSink, &LIBID_someLib, 1, 0>
{
public:
CObjEventSink(T* pOwner) : m_pOwner(pOwner) {}
BEGIN_SINK_MAP(CObjEventSink)
SINK_ENTRY_EX(0, DIID__IEventSink, 0x1, OnEvent)
END_SINK_MAP()
void STDMETHODCALLTYPE OnEvent() {m_pOwner->OnEvent();}
T* m_pOwner;
};
In the main objects header file I define two vectors, one for the objects
and another for the event sinks.
// define a collection of objects and event sinks
std::vector<CComVariant> m_vecObjs;
std::vector< CObjEventSink <CSomeObj> > m_vecObjSinks;
In the implementation file of the main object I create the collection of the
inner objects, event sinks and establish the connections.
for (i=0; i<10; i++) {
// create inner objects
CComPtr<ISomeObj> pobj;
pobj.CoCreateInstance(__uuidof(SomeObj));
m_vecObjs.push_back(CComVariant(pobj));
// create event sink for inner object
m_vecObjSinks.push_back(this);
m_vecObjSinks[i].DispEventAdvise(pobj);
}
Now here is my problem when I fire the event from one of the inner objects
the program crashes in the connection point proxy class
HRESULT Fire_Event()
{
T * pThis = static_cast<T *>(this);
???
???
pThis->Lock();
CComPtr<IUnknown> punkConnection = m_vec.GetAt(iConnection);
pThis->Unlock();
???
???
???
}
And when I step into the code to find out the exact point of failure it???s in
atlcomcli.h at the Addref() in the following code;
CComPtrBase(T* lp) throw()
{
p = lp;
if (p != NULL)
p->AddRef();
}
Now, when I create only one instance of the inner object and one event sink
(not as a collection) as in the following. The code works perfectly the event
is fired and handeled by the main object.
// main objects header file
CComPtr<ISomeObj> m_Obj; // single object
CObjEventSink <CSomeObj> m_ObjSink; // single sink
// main objects constructor
CMainObj() : m_ObjSink (this)
{
}
// main objects final construct
m_Obj.CoCreateInstance(IID_ISomeObj);
m_ObjSink.DispEventAdvise(m_Obj);
My question is am I not declaring the vector of event handlers properly, why
would it work with a single object but not a collection of objects?