Bug analysis: What might cause an int to be replaced by random digits?
Short version: There is a C# variable of type int (32-bit signed integer). It gets replaced by seemingly random digits. It is non-deterministic. The real answer is in the comments.
If this were C or C++, I would think random memory was being accessed and interpreted as a 32-bit signed integer. But this is C#, it should be type-safe and memory-safe.
I only got to look at the source code for 3 minutes, and someone else was moving the scroll bar and picking which source file to view (the developer was showing me the code). I did not see the problem. I will have full access to the source code in a few days, so the "real answer" will almost definitely become known, and I will post back.
In the meantime, I am burning with curiosity. The developer has no idea. A friend of mine said that it may be a hash value. Instead of using the integer that was entered, the variable may be set to the value of some hash function which is normally used for a Hashtable or Dictionary. My friend said he had seen such a bug, but I am struggling to imagine how it was created by accident.
I am still wondering if it is possible that the value is being read from uninitialized memory, or memory that was overwritten by something else.
Ideas? Wild speculation?
EDIT: I have gained access to the source code. I did a multi-file search on the variable name and immediately found the problem. I am going to add the real cause of the problem as a comment.
Posted Jul 21, 2017, 9:56 PM UTC · edited