I have an Animal
base class that I use in some simulation programs. In this case there may be up to 500 Animal
s in a run. Each run is set up to have each Animal
do something each "time step". So I just loop through the list of animals , call DoTimeStep
on each one until all the time steps are done for the run.
Each Animal
class has its own logger class to write out data for each "time step" in the simulation. This way each Animal
has its own log file. It worked fine forever (3 years), until we tried to run it on a virtual machine. Then for what ever reason every once and a while the logger reference will be null for a "time step" and then the next time it will be there. The really strange part is the StreamWriter
inside the logger never seems to lose track of where its file is. It just skips writing out the line for that time step. And the error log shows a NullReferenceException
on the Logger
class.
I can not find any pattern for this behavior. The Animal
class was not destroyed and recreated. The logger is created in the Animal
constructor and destroyed in IDispose
. Any ideas on how I would start to debug this issue?
Edit: I can recreate this will only 3 animals, so the 500 open files should not be it. But thanks for trying.
Edit: I am not sure what I am supposed to do when I catch the error for the Null Exception . I was already catching it, but I can not figure how to find out why it is happening. Sorry for seeming obtuse. As an aside I did try the Thread.Sleep(300) for 10000 loops to see if there was some sort of race going on that I was unaware of. It never became not null in the loop. But 3 seconds later when I had cycled through the other two animals and came back it was no longer null.