Вопрос

I got a strange exception at line 89 of the following code:

    private byte   state = 0;
    .
    .
    .

    void OnGUI(){
    int i;
    GUI.skin = skin;
    GUI.skin.box.fontSize = 25;
    GUI.skin.label.fontSize = 18;
    if(state==0){           
        GUI.skin.font = system;
        GUIContent tA = new GUIContent("A");
        Vector2 tz = GUI.skin.label.CalcSize(tA);
        GUI.skin.label.CalcHeight(tA,tz.x);
        height = (byte) tz.y;
        rows   = (byte) (Screen.height/height);
        mark   = 0;
70      view   = new Log[rows];
        for(i=0;i<rows;i++){
72          view[i] = new Log();
            if(vis.ValidRow()){
74              mark++;
75              view[i].time = vis.time;
76              view[i].descrip = vis.descrip;
                vis.Next();
            }
        }
        state = 1;
    }

    GUI.skin.font = chrome;
    GUI.Box(new Rect(0,0,Screen.width,50),"Server");
    GUI.color = Color.green;
    GUI.skin.font = system;
    short y = 50;
    for(i=0;i<mark;i++){
89      GUI.Label(new Rect(10,y,200,height),String.Format("{0:yyyy/MM/dd HH:mm:ss}",view[i].time));
        GUI.Label(new Rect(220,y,Screen.width-260,height),view[i].descrip);
        y += height;
    }
}

System.NullReferenceExeption is thrown, caused due to view being null, but I just can't figure out why. All variables have the correct value except for view. In this case state is 1, which means the code inside the if was executed, mark is 3, which means lines: 70, 72 and 74 were executed without problem. If for some reason new would've failed at line 70, the exception would've been thrown at line 72. Am I right?... But lines 75 and 76 were also executed without problem.

The only reasonable explanation I can find for this behavior is that view was garbage collected, but can't understand why... and if that's the case, how can I keep view from being garbage collected?

EDIT:

There is no other place in the program where state is set to 1 nor any other value. view is never set to null anywhere in the program. What you see is the whole functional code, I only removed variable declarations and initialization, none of which touches either state nor view. Both state and view are declared private.

This code is a test project I'm doing in Unity.

EDIT:

This is not a fixed problem, it happens only once in a while, and it seems there is some kind of timing involved, since until now I have been completely unable to repeat this problem with the debugger on.

Это было полезно?

Решение 2

I still don't know why this problem happened, and probably never will. I modified OnGUI so the if is rather controlled by view==null instead of state==0, this way if for some strange reason view becomes null it is restored.

Другие советы

From what I can see, mark is counting HOW MANY array slots have values, while the index by which they're assigned is I. So, this is one scenario that can happen:

null, null, null, 1, null, 0, 2

where mark is 3, and rows is 7. Your second for counts to mark, accessing the first three null elements.

I know you're setting every entry in the array to new Log(), but this is a very valid logical error. Also, maybe the object which is null is not view OR view[I], but rather view[I].time? Do a line-by-line trace of the code and we'll know for sure.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top