I wrote a windows service in C++ that needs to restart every night at midnight, so I call exit(1) on it so that it can be restarted by SCM. The problem is it seems every other night it starts up partially and hangs. In the event log, I get this:
Application popup -Application Error: The instruction at "0x0043c145" referenced memory at "0x00000035". The memory could not be "read".
It seems to fail right before opening an ODBC connection to a SQL Server 2008 database. I can confirm the service actually exits before it restarts; nevertheless I get this error every once in a while when it stops and restarts itself, but if I stop and restart the service manually over and over I can never get it to fail, and if I control the process from a terminal port and exit manually from there it never fails either.
If I try to attach a debugger the process quits, so I can't glean any useful information that way either.
I'm tearing my hair out trying to figure out what's going on, but I don't know where to start. Anyone have any ideas?