
URGENT: STL std::map<ULONG, CEvent>
In general, it's incorrect.
Handles get copyed/duplicated with the DuplicateHandle() API.
You are not implementing an assignment operator, and,
your SC++L implementation may use it,
force the compiler to syntethyze one for you, with bitwise copy semantic.
Adding an element to a map by using the std::map<>::operator[]
is 'obscure', to say the least, but if you read the implementation,
you will see that the temporay object passed to the insert() method
is destoyed at the end of the expression, and since bitwise copy semantic is
used,
the container will have a bitwaise copy of an invalid handle.
when the container goes out-of scope, you will close an invalid handle.
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of any included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
Quote:
> Hello,
> Here is my Class CEvent that I use in my std::map.
> ***************************************
> DEFINITION:
> class CEvent
> {public:
> CEvent();
> ~CEvent();
> // Copy Constructor
> CEvent(const CEvent&);
> private:
> HANDLE m_hEvent;};
> IMPLEMENTATION:
> static HANDLE Create();
> CEvent::CEvent()
> : m_hEvent(Create()){}
> // Copy Constructor
> CEvent::CEvent(const CEvent& rhs){
> m_hEvent = rhs.m_hEvent;} // IS THIS INCORRECT?
> CEvent::~CEvent()
> { ::CloseHandle(m_hEvent);}
> // Helper function
> static HANDLE Create()
> { HANDLE handle = ::CreateEvent(0,true,false,0);
> return handle;}
> *****************************************
> and my main()
> *****************************************
> #include <map>
> #include "CEvent.h"
> int main()
> {
> typedef std::map<ULONG,CEvent> HANDLELIST;
> HANDLELIST handleList;
> HANDLELIST::iterator ihandleList;
> handleList[1]; // ERROR HERE!!
> return 0;}
> ******************************************
> I get the following error (no error during compilation) when running the
> code:
> First chance exception in Program.exe (NTDLL.DLL): 0xC0000008: Invalid
> Handle
> I think the code is trying to delete the same memory twice.
> Is my copy constructor incorrect?
> Thanks,
> Sami