Overview
Features
Download
Documentation
Community
Add-Ons & Services

Optimizer/Bad AutoPtr question

A general discussion forum.

Optimizer/Bad AutoPtr question

Postby Royce » 19 Aug 2014, 00:03

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
Royce
 
Posts: 19
Joined: 23 Feb 2012, 18:13

Re: Optimizer/Bad AutoPtr question

Postby alex » 19 Aug 2014, 01:39

It's kindof hard to figure out without seeing the code, but here is where the problem likely is:

When you assign a pointer to AutoPtr, AutoPtr will take ownership of it and reference count is set to 1. When AutoPtr is destroyed, release() will be called on the held pointer and, if reference count is one, it will be decremented to zero and delete is called on the pointer. If the original pointer was deleted before, the problem is here - the original pointer was deleted before AutoPtr is destroyed and it will call delete on the dangling pointer. If you hold a naked pointer somewhere else, you must assign it to AutoPtr with AutoPtr(ptr, true) constructor to let it know it is shared. And then, never directly call delete but only release() on the original naked pointer so the reference count is kept accurate. Generally speaking, mixing naked pointers and AutoPtr is tricky; it can be done, but make sure you (a) never assign AutoPtr back to a naked pointer and (b) never directly delete the naked pointer. Needless to say, never assign a pointer to stack variable to AutoPtr.
alex
 
Posts: 1146
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Optimizer/Bad AutoPtr question

Postby Royce » 19 Aug 2014, 01:53

I wish I could show the code. :cry:

Anyway, there are a couple places where I turn the this pointer into a fresh AutoPtr, but I'm careful to pass in the true to get that duplicate call on construction. I'm sure I find a spot where I lose control of the naked pointer or something. I'm just having a devil of a time seeing it. That's why I was looking for other possible solutions.

Thanks!
Royce
 
Posts: 19
Joined: 23 Feb 2012, 18:13

Re: Optimizer/Bad AutoPtr question

Postby alex » 19 Aug 2014, 02:03

Royce wrote:there are a couple places where I turn the this pointer into a fresh AutoPtr, but I'm careful to pass in the true to get that duplicate call on construction.


Are you calling delete on that pointer anywhere? If so, change it to release().
alex
 
Posts: 1146
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Optimizer/Bad AutoPtr question

Postby Royce » 19 Aug 2014, 02:24

I've done text searches for the word delete and came up empty in any part of the code that deals with these connections. I has to be something more subtle. I remember being surprised by the assignment to a std::auto_ptr over on the TCPServer side, but that doesn't happen in the reactor.

I'm just missing it. I'll keep looking. Maybe tomorrow when I'm fresh, I'll find it and wonder how I didn't see it immediately.

Thanks again for your time.
Royce
 
Posts: 19
Joined: 23 Feb 2012, 18:13


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest