Windows, 32bit, VS2010, Static, Release.
I have a slightly off standard socket reactor setup that holds AutoPtrs to connection objects in a std::set. It seemed to work alright for a good long while, but then the service started getting a lot of clients that never ever closed their sockets and I started running into trouble with FD_SETSIZE.
So I made each connection object record the last time they received something and added a thread and static method to the object that holds the AutoPtrs to the connection objects. Once a minute that thread locks the set and scans each object in the set to see the time of the last reception. If it is more than a minute it copies the AutoPtr and puts it (additionaly) in to a second, temporary, set of idle connections. Another loop over that temporary set that follows immediately afterward initiates a process whereby the underlying socket is closed, and the connection object removed from the primary connection set.
This seemed OK too, but then, once, I got a WaitForCriticalSection crash in that new thread while trying to lock the Timestamp inside the connection I was querying for last-time-of-reception. It seems clear that the AutoPtr was pointing to memory that had been deleted. The logs certainly suggest that. I think the client closed the connection.
Certainly there is a good chance that somewhere I'm letting the underlying pointer get out from control of the AutoPtr regime. I can't find that, but I'll keep looking for that.
However, one thing I wonder about is the way that new thread accesses the primary set of connection AutoPtrs. The thread runs in a static method in the object that has the primary set as its member. I pass in the pointer to the relevant object instance as part of starting the thread on that static method and then then thread casts that void* to a typed "me" pointer that the static method forevermore uses to get at the primary set and its associated lock.
Is it possible that optimizer caused that thread to access a copy of the primary set and not the set itself? Would declaring the "me" pointer volatile cure that?
Thanks for your time