Passing object with null member

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news@mail.adsl4less.com

    Passing object with null member

    ..Net 2.0

    Hi, I'm getting a nullreferenceex ception when calling a function from a
    worker thread:

    private void CallbackProc(st ring response, Exception ex)
    {
    if (this.InvokeReq uired)
    {
    this.Invoke(new MyCallback(Call backProc), new object[] {
    response, ex });
    }
    else
    {
    // do stuff here on the ui thread
    }
    }

    It is true that ex is indeed null in some calls of CallbackProc, but
    not always. The exception is thrown whenever ex is indeed null.
    However, the documentation doesn't say that I can't pass null in an
    Invoke (indeed it seems to say the opposite). So, I tried it out with a
    fresh project but this time from one thread. (I.e. no worker thread.)
    Ok, it was a hack because I replaced the "this.Invokereq uired" with a
    flag to ensure it only looped once. Anyway, the point is that it
    worked: Invoke was happy to have a null argument in the object array.

    My final test was to enclose the Invoke in a try catch. Surprisingly,
    although the exception is still thrown, the Invoke call is still made
    and works exactly as I want it to! Weird!

    I could leave it with the try catch but that's horrible. So, can
    someone point me in the right direction please?

    TIA

  • Jon Shemitz

    #2
    Re: Passing object with null member

    news@mail.adsl4 less.com wrote:
    Hi, I'm getting a nullreferenceex ception when calling a function from a
    worker thread:
    >
    private void CallbackProc(st ring response, Exception ex)
    {
    if (this.InvokeReq uired)
    {
    this.Invoke(new MyCallback(Call backProc), new object[] {
    response, ex });
    }
    else
    {
    // do stuff here on the ui thread
    }
    }
    This should be valid. Is it the Invoke that's throwing the exception,
    or is it simply relaying an exception thrown by the "do stuff here on
    the ui thread" code?

    --

    ..NET 2.0 for Delphi Programmers

    What you need to know.

    Comment

    • news@mail.adsl4less.com

      #3
      Re: Passing object with null member

      Jon Shemitz wrote:
      >
      This should be valid. Is it the Invoke that's throwing the exception,
      or is it simply relaying an exception thrown by the "do stuff here on
      the ui thread" code?
      >
      It is the Invoke that's complaining, and in particular the fact that ex
      is null. If I force populate ex with a new Exception object (the
      problem, of course, occurs whatever object type I choose) just before
      the Invoke, it works without complaining. Very odd indeed.

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Passing object with null member

        <news@mail.adsl 4less.comwrote:
        This should be valid. Is it the Invoke that's throwing the exception,
        or is it simply relaying an exception thrown by the "do stuff here on
        the ui thread" code?
        >
        It is the Invoke that's complaining, and in particular the fact that ex
        is null. If I force populate ex with a new Exception object (the
        problem, of course, occurs whatever object type I choose) just before
        the Invoke, it works without complaining. Very odd indeed.
        Could you post a short but complete program which demonstrates the
        problem?

        See http://www.pobox.com/~skeet/csharp/complete.html for details of
        what I mean by that.

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • Jon Shemitz

          #5
          Re: Passing object with null member

          news@mail.adsl4 less.com wrote:
          This should be valid. Is it the Invoke that's throwing the exception,
          or is it simply relaying an exception thrown by the "do stuff here on
          the ui thread" code?
          >
          It is the Invoke that's complaining, and in particular the fact that ex
          is null. If I force populate ex with a new Exception object (the
          problem, of course, occurs whatever object type I choose) just before
          the Invoke, it works without complaining. Very odd indeed.
          Have you tried commenting out the "do stuff here on the ui thread"
          code? The code below works just fine, for me. If I uncomment the
          `throw new NullReferenceEx ception()` line, I do get a
          NullReferenceEx ception on the Invoke.

          using System;
          using System.Windows. Forms;
          using System.Threadin g;

          namespace InvokeIssue
          {
          public partial class Form1 : Form
          {
          public Form1()
          {
          InitializeCompo nent();
          }

          private void Form1_Click(obj ect sender, EventArgs e)
          {
          ThreadPool.Queu eUserWorkItem(d elegate
          {
          CallbackProc("I n thread", null);
          });
          }

          delegate void MyCallback(stri ng response, Exception ex);

          private void CallbackProc(st ring response, Exception ex)
          {
          if (this.InvokeReq uired)
          {
          this.Invoke(new MyCallback(Call backProc), new object[]
          {
          response, ex
          });
          }
          else
          {
          // do stuff here on the ui thread
          //throw new NullReferenceEx ception();
          MessageBox.Show (
          String.Format(" response = {0}\nex is {1}null",
          response, ex == null ? "" : "not "),
          "CallbackProc") ;
          }
          }
          }
          }

          --

          ..NET 2.0 for Delphi Programmers

          What you need to know.

          Comment

          • Bruce Wood

            #6
            Re: Passing object with null member


            news@mail.adsl4 less.com wrote:
            Jon Shemitz wrote:

            This should be valid. Is it the Invoke that's throwing the exception,
            or is it simply relaying an exception thrown by the "do stuff here on
            the ui thread" code?
            >
            It is the Invoke that's complaining, and in particular the fact that ex
            is null. If I force populate ex with a new Exception object (the
            problem, of course, occurs whatever object type I choose) just before
            the Invoke, it works without complaining. Very odd indeed.
            Don't forget that if your callback method fails when you pass it an ex
            == null, then the final exception you get will be from the Invoke, with
            the true exception that caused the problem as its InnerException.

            I've run across this many times. I look at the exception and think,
            "Oh, I've screwed up the Invoke," when in fact I haven't: the error is
            in the code that was Invoked, and it's just the way that exceptions are
            handled across an invocation that makes it look otherwise.

            Comment

            • news@mail.adsl4less.com

              #7
              Re: Passing object with null member

              Bruce Wood wrote:
              >
              Don't forget that if your callback method fails when you pass it an ex
              == null, then the final exception you get will be from the Invoke, with
              the true exception that caused the problem as its InnerException.
              >
              I've run across this many times. I look at the exception and think,
              "Oh, I've screwed up the Invoke," when in fact I haven't: the error is
              in the code that was Invoked, and it's just the way that exceptions are
              handled across an invocation that makes it look otherwise.
              Bingo - problem solved! InnerException was null, but the Invoke was
              indeed complaining about an a null exception in the _else_ statement.
              (The debugger doesn't help because I can't "step into" the invoke even
              with F11 - it's always the Invoke that throws the exception in the IDE.
              Maybe I'm not using it right?) I pared the code down until I had the
              bare essentials throwing the exception. My schoolboy mistake was then
              obvious. :) For anyone else following this, here's the bug. Thanks to
              Jon, Jon and Bruce for your help.



              using System;
              using System.Windows. Forms;
              using System.Threadin g;

              namespace WindowsApp
              {
              public delegate void MyCallback(Exce ption ex);

              public partial class Form1 : Form
              {
              public Form1()
              {
              InitializeCompo nent();
              }

              private void Form1_Click(obj ect sender, EventArgs e)
              {
              Worker worker = new Worker(new MyCallback(Call backProc));
              Thread thread = new Thread(new
              ThreadStart(wor ker.FireItUp));
              thread.Start();
              }

              public void CallbackProc(Ex ception ex)
              {
              if (this.InvokeReq uired)
              {
              this.Invoke(new MyCallback(Call backProc), new object[]
              { ex });
              }
              else
              {
              // The following line is the bug as ex can be null.
              D'oh!
              Console.WriteLi ne(ex.Message);
              }
              }
              }

              public class Worker
              {
              public MyCallback _callback;

              public Worker(MyCallba ck callback)
              {
              _callback = callback;
              }

              public void FireItUp()
              {
              _callback(null) ;
              }
              }
              }

              Comment

              • news@mail.adsl4less.com

                #8
                Re: Passing object with null member

                Bruce Wood wrote:
                >
                Don't forget that if your callback method fails when you pass it an ex
                == null, then the final exception you get will be from the Invoke, with
                the true exception that caused the problem as its InnerException.
                >
                I've run across this many times. I look at the exception and think,
                "Oh, I've screwed up the Invoke," when in fact I haven't: the error is
                in the code that was Invoked, and it's just the way that exceptions are
                handled across an invocation that makes it look otherwise.
                Bingo - problem solved! InnerException was null, but the Invoke was
                indeed complaining about an a null exception in the _else_ statement.
                (The debugger doesn't help because I can't "step into" the invoke even
                with F11 - it's always the Invoke that throws the exception in the IDE.
                Maybe I'm not using it right?) I pared the code down until I had the
                bare essentials throwing the exception. My schoolboy mistake was then
                obvious. :) For anyone else following this, here's the bug. Thanks to
                Jon, Jon and Bruce for your help.



                using System;
                using System.Windows. Forms;
                using System.Threadin g;

                namespace WindowsApp
                {
                public delegate void MyCallback(Exce ption ex);

                public partial class Form1 : Form
                {
                public Form1()
                {
                InitializeCompo nent();
                }

                private void Form1_Click(obj ect sender, EventArgs e)
                {
                Worker worker = new Worker(new MyCallback(Call backProc));
                Thread thread = new Thread(new
                ThreadStart(wor ker.FireItUp));
                thread.Start();
                }

                public void CallbackProc(Ex ception ex)
                {
                if (this.InvokeReq uired)
                {
                this.Invoke(new MyCallback(Call backProc), new object[]
                { ex });
                }
                else
                {
                // The following line is the bug as ex can be null.
                D'oh!
                Console.WriteLi ne(ex.Message);
                }
                }
                }

                public class Worker
                {
                public MyCallback _callback;

                public Worker(MyCallba ck callback)
                {
                _callback = callback;
                }

                public void FireItUp()
                {
                _callback(null) ;
                }
                }
                }

                Comment

                Working...