1

I have some problems with System.NullReferenceException. When server do things and we first check user is not null and then he disconnect and server progress something and it try get user comes System.NullReferenceException. I have over 20 000 lines code so i need something small not like allways check is it null.. My server is multithread so sockets get connections and disconnects users alltime on backround so thats why this comes.. I want stop that progress when user disconnect. If i put everywhere "try/catch" is that goodway?

Example:

if (User != null)
{
    //do some things
    System.Threading.Thread.Sleep(1000); //now we have time disconnect (This only for get error)
    User.SendMessage("crash"); //<-- System.NullReferenceException... -.-
}
user3290224
  • 77
  • 2
  • 8

3 Answers3

4

It looks like the value of User is changing after the test but before the call to SendMessage. If your application is multithreaded I suspect that another thread is setting User to null whilst you are sleeping.

One option would be to grab User and check against it:

var user = User;
if (user != null)
{
    //do some things
    System.Threading.Thread.Sleep(1000);get error)
    user.SendMessage("crash"); // Need to be sure user is in a valid state to make call
}

This way you can be sure that you've got a valid reference. What you'll now need to do is ensure that user is in a valid state for you to call SendMessage.

UPDATE: Since you're keep to avoid adding try/catch blocks you could write a helper function:

void WithUser(Action<User> action)
{
  try
  {
     var user = User;
     if (user != null) action(user);
  }
  catch(Exception e)
  {
    // Log it...
  }
}

Now you can say:

WithUser(user=>
{
  System.Threading.Thread.Sleep(1000);
  user.SendMessage("crash");
});
Sean
  • 60,939
  • 11
  • 97
  • 136
  • Hmmm.. is that var goodway because example User.GetClient() is called too so it returns too null if user disconnect – user3290224 Feb 21 '14 at 08:38
  • @user3290224 - I don't really understand your comment, but this is a reasonable way to handle properties becoming `null` in a multithread application. What you do need to do is ensure that the object is in a valid state when calling against it, so you'll probably have to surround it with a `try/catch` block. – Sean Feb 21 '14 at 08:40
  • try/catch for 20 000 lines? I mean there is too other functions that become null when user disconnect and server need it other times example that GetClient() – user3290224 Feb 21 '14 at 08:42
  • Sorry my slow answer because i goed sleep. Is that goodway use it with 20 000 codes line? There is many and long functions that uses many time User etc. Can "try" make my server slower? – user3290224 Feb 21 '14 at 16:10
  • It won't affect performance unless you have an exception. You need to stop worrying about how much code you've got (and 20,000 isn't that much) and start thinking about how you'll make your application bug free. It's not like you have to try\catch each line of code, is it...! – Sean Feb 21 '14 at 16:13
  • I forgot i have many try/catch like in map handler but it makes cpu 100% when many errors comes ;/ – user3290224 Feb 21 '14 at 16:16
  • I can't comment much on that as I've not seen the code, but if you're throwing so many exceptions that you've maxed out the CPU then you're doing something wrong. – Sean Feb 21 '14 at 16:17
  • Idk why but if comes too many System.NullReferenceException cpu goes higer and higer and then its 100% -> server laggggggs there is already try/catch so why copu goes 100%? – user3290224 Feb 21 '14 at 16:19
  • If you're getting that many NullReferenceExceptions then I suspect the architecture of your program is wrong. If sounds like you're creating a lot of objects then closing them and trying to write to them at the same time. It sounds like the closing logic is flawed. – Sean Feb 21 '14 at 16:22
0

SendMessage is throwing null exception, check code in your SendMessage Method

if User goes null before call dont call SendMessage:-

 if (User != null)
 {
       User.SendMessage("crash"); //<-- No More System.NullReferenceException... -.-
 }
Vinay Pandey
  • 8,589
  • 9
  • 36
  • 54
0

Who is setting User to null and where? While your thread sleeps, somebody is obviously setting the User variable to null, causing the crash.

Besides: "Magical sleeps" will not solve any problems. You need proper locking here.

PMF
  • 14,535
  • 3
  • 23
  • 49
  • Socket disconnect set user to null and my program is multithread so it can happend any time. That sleep is example because its hard try get that error because server do things in ms but that dont stop coming over 100 System.NullReferenceException every day ;/ – user3290224 Feb 21 '14 at 08:33
  • Then you need to implement proper locking. Testing for null alone will not do in a multithreaded environment. – PMF Feb 21 '14 at 09:00