2

I have the following code:

try {
    using (var stream = new MemoryStream()) {
        var ms = stream;
        if (control is DockLayoutManager) {
            if (control.Dispatcher == null || control.Dispatcher.CheckAccess()) {
                ((DockLayoutManager)control).SaveLayoutToStream(ms);
            } 
        }
    }
} catch (Exception e) {
    log.Error(string.Format("Cannot GetLayout ({0}).", typeName), e);
}

From time to time, I get a NullReferenceException on the line

((DockLayoutManager)control).SaveLayoutToStream(ms);

I have no idea, why there can be a NullReferenceException on this line.

I hope, someone can help me.

NotMe
  • 87,343
  • 27
  • 171
  • 245
BennoDual
  • 5,865
  • 15
  • 67
  • 153
  • 3
    Put a breakpoint on that line. Is anything `null` at that point? Simple use of the debugger can fix 99.99% of `NullReferenceException`s. – Simon Whitehead Jan 04 '14 at 22:31
  • 1
    Shouldn't it be `if (control!=null && control.Dispatcher!= null && control.Dispatcher.CheckAccess()) {` – L.B Jan 04 '14 at 22:31
  • 1
    Maybe it's a bug in `SaveLayoutToStream`? Show the stack trace. – Dan Abramov Jan 04 '14 at 22:31
  • Won't `((DockLayoutManager)control)` return null if the cast fail? – Brandon Buck Jan 04 '14 at 22:32
  • Make more sub methods and get the stacktrace. Possibly Control can be a DockLaoyutManager but have a value of null. Also there can be bugs in getters of properties. Sometimes they are caused by illegal cross thread calls. What StackTrace do you currently get? – Boas Enkler Jan 04 '14 at 22:32
  • 2
    @izuriel no, if cast failed it would throw exception. – L.B Jan 04 '14 at 22:32
  • @izuriel No it would throw an `InvalidCastException` Exception – Andy Jan 04 '14 at 22:35
  • A cast that fails because the object cannot be cast to `DoctLayoutManager` will throw an `InvalidCastException`. A cast of `null` to another reference type will simply return `null`. – Lasse V. Karlsen Jan 04 '14 at 22:35
  • Do you mean _not_ NULL _and_ CheckAccess passes? – HABO Jan 04 '14 at 22:35
  • How certain are you that the line you've said throws that exception is the actual culprit? – Lasse V. Karlsen Jan 04 '14 at 22:38
  • 3
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jan 04 '14 at 22:42
  • 1
    It would appear that, according to the answer by Chris below.. my "debugger can fix 99.99% of NulLRefs" comment puts this question into the 0.01% :)' – Simon Whitehead Jan 04 '14 at 22:45

3 Answers3

4

The problem is inside .SaveLayoutToStream(ms)

This has happened several times before:

https://www.devexpress.com/Support/Center/Question/Details/B190607 http://www.devexpress.com/Support/Center/Question/Details/B221485 https://www.devexpress.com/Support/Center/Question/Details/Q445171

If you have the source code from them, then I'd link those into the solution, rebuild and trace through when it failed. Also I'd make sure my source/binaries were completely up to date.

If you don't, then debugging this is going to be extra hard. DevExpress has indicated they have no desire to put the code in place to throw a solid enough error for you to be able to pinpoint the exact cause.. Also, the reason why the compiler is throwing on that line is simply that it doesn't have any further source code lines to point you to and it's happening inside that method. In which case, contact DevExpress to ask them what's up.

Seems to be a serialization issue. The B221485 issue number appears to indicate that having a control with a property of type DefaultBoolean is being set to -1 and subsequently blowing up. Support said to locate any of those properties where you are setting such a property to true or false as being indicators of what to fix. seems odd.

NotMe
  • 87,343
  • 27
  • 171
  • 245
  • 1
    +1 For a nice Google search! (I didn't realise this was DevXpress.. the thought of there being a bug in the method seemed ridiculous to me until I saw that!) – Simon Whitehead Jan 04 '14 at 22:43
1

If this code behaves like it obviously appears to do, this line cannot throw a nullref. Something else is going on. Put a breakpoint there and observe runtime behavior.

Ideas:

  1. Something in control.Dispatcher.CheckAccess could set control to null. That would be horrible code indeed.
  2. It is a threading bug (a race).
  3. Outdated source file. Rebuild the solution.
  4. Something inside of SaveLayoutToStream threw and you misinterpreted the location exception. Look at the stack trace to find out. Set the debugger to break on exceptions (Ctrl-Alt-E).
usr
  • 168,620
  • 35
  • 240
  • 369
0

Looks like a race condition to me, e.g. something changed the control after the if check.

By the way, it will be recommended to do something like:

var dockLayoutManager = control as DockLayoutManager;

if (dockLayoutManager != null) {
        if (dockLayoutManager.Dispatcher == null || dockLayoutManager.Dispatcher.CheckAccess()) {
            dockLayoutManager.SaveLayoutToStream(ms);
        } 
    }

So you don't have to cast twice.

Gang Gao
  • 1,051
  • 8
  • 9