开发者

C# : Obscure object ref error

Have an app which has been running fine for a while. All of a sudden it has started throwing intermitent "Object reference not set...." errors at a certain line of code on the product server.

I started the app in Visual Studio and debugged it with a breakpoint at the offending line of code and have replicated a really strange behaviour.

Initially when VS breaks at the line of code breakpoint, the variable is NULL but if I wait 5 secs or so then the variable is no longer NULL. This can be sped up by calling the variable in the Immediate Window in VS which causes the NULL field to populate or resolve instantly. This is the exact intermitent nature of the error on the production server.

Any ideas where to start? The data is loaded into the variable before hitting the method but for some reason there is a lag in referencing the variable in memory. Heaps of available memory on dev and production servers.

Really strange...need help to locate a place to start to resolve it.

Thanks in advance.

Info : .NET 3.5, VS 2008

Code :

public static List<Model> CreateModel(List<subModelA> subModelAs, List<subModelB> subModelBs, int duration, bool isGroup) {
            List<Model> result = new List<Model>();

            try {
                if (subModelAs != null && subModelAs.Count > 0) {
                    if (subModelBs != null && subModelBs.Count > 0) {

                        subModelBs.ForEach(b => {
                            subModelA a = subModelAs.Find(x => x.id == b.comp[0].subModelAComp.subModel.id);
                            result.Add(CreateNewModel(a, b, duration));
                        });
                    }
                }
            }
            catch (Exception ex) {
                throw ex;
            }
            return result;
        }

The following line is the source of the issue :

subModelA a = subModelAs.Find(x => x.id == b.comp[0].subModelAComp.subModel.id);

If Sometimes b is NULL and other times subModelAComp is NULL. If i breakpoint this line and in the Immediate Window if i execute "b.comp[0].subModelAComp" it is NULL, i then execute "b" and then execute "b.comp[0].subModelAComp" again and it is no longer NULL. The call to "b" seems to force the "subModelAComp" to not be NULL. There is n开发者_运维百科o code in the getters which could cause this change.

*** The application is not threaded. No multi-threading implemented ****


Without seeing any code, it's pretty hard to say, but two possibilities:

  • Is this definitely a variable, or might it be a property? If it's a property, evaluating it once may give a non-null value, but the second evaluation could be null

  • Are there multiple threads involved? If so, there are two potential problems:

    • If the data is being set in one thread but read in another, the "setting" thread may have written it, but without the "reading" thread reading it, due to caching etc. There are various ways around this - the simplest being to always acquire a lock before accessing shared data. (Using the same lock for every access to the same piece of data.)
    • Another thread could be setting the value to null - so you'd see it be non-null, and then later null

Any code you could show us would help.


Hm, my first guesses would be:

  • Is your application multi-threaded? Could another thread be accessing the variable? During debugging, check the “Threads” debug window and try freezing all the threads except the current one, then see if the variable contents still change unexpectedly.

  • Is it really a variable? Or could it be a property getter that has crazy side-effects? The debugger would trigger those side-effects too when you query the property from the “Watch” window or the “Immediate” window.


If it's a property, put a breakpoint on the property setter, so you can view the stack trace to see what is setting it to null.


Any ideas where to start? The data is loaded into the variable before hitting the method but for some reason there is a lag in referencing the variable in memory.

To me, that assertion smells.

Are you sure that the data is loaded into the variable before hitting the method? Perhaps you haven't written any multithreading code, but you may be using some .NET class with asynchronous behavior (events, etc.) I would take this assumption out and debug from there. (Meaning, I'd start by setting a breakpoint before the .ForEach calls, or even before CreateModel is called and check each "b" in the list manually)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜