×

Discussion Board

Results 1 to 3 of 3

Thread: gc error?

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    11

    gc error?

    Can anyone help me with a very strange error I am getting? What happens is:

    I read some binary data from an http conection into a byte array and return it:


    int buffLen=2;
    serverData=new byte[buffLen];
    DataInputStream dis=new DataInputStream(is);
    dis.read(serverData,0,buffLen);
    serverData=new byte[ buffLen=(((int)serverData[1])&0xff)|((((int)serverData[0])<<8)&0xff00) ];
    dis.read(serverData,0,buffLen);
    dis.close();
    dis=null;
    is.close();
    is=null;
    conn.close();
    conn=null;
    System.out.println("CONNECTED OK");
    return serverData;

    //note the first 2 bytes are the length of the rest of the data.


    the function returns and the calling function parses the data after printing it. At least it should..

    System.out.println("serverData"+serverData.length);
    for(int i=0;i<serverData.length;i++){
    System.out.println("sd:"+i+":"+serverData[i]);
    }//end for
    System.out.println("DonePrintingServerData");


    this prints the following:

    serverData6042
    sd:0:5
    sd:1:1
    sd:2:64
    sd:3:0
    ...
    sd:1283:-60
    sd:1284:32
    sd:1285:60
    sd:1286:68

    and then it stops..

    the emulator screen says there is a null pointer exception. It never prints "DonePrintingServerData" or even reaches the 6042th

    byte. Just stops there. It also does not print any of the many e.printStackTrace()'s from the try/catch blocks that it is

    wrapped in at different levels, not even the one in the run() method inside of which all function calls originate. Which is to

    bad because I also have it set up to display a message on the actual phone, and prevent a complete app crash should any of those

    catch blocks catch anything. (with this bug it doesnt seem to matter)

    If I comment out the printing section it works fine on the EMULATOR and parses everything perfectly. It parses ALL of the data

    that is comming in from my servlet, and it does it correctly.

    So as wierd as that seems, I try running it on an actual phone after removing the System.out.println()'s and that print loop. I

    get the same null pointer error at the same point in execution. I can tell that it is in that point of execution because of were

    the app is when it crashes, not because of any thing I get back, because all i get back is the white default null pointer

    exception screen.

    I am at a loss here. There are no other threads working in my app. It doesnt matter if I make the byte array a static data

    member of the class, or if I return it, or even if I make a byte[][] serverData=new byte[1][] and pass it into the http request

    function and assign the data to serverData[0]=byteArrayData; to bypass how java passes references by value to return it through

    a function paramater. But its as if the array is garbage collected when it shouldnt be or something. The only things refrenced

    in that loop is the serverData[] and System.out. How could this happen?

    One of our other programmers thinks that the array is not totaly inintialized, but its a byte array, not an object array. I dont

    know, if any one has any ideas I would greatly appreciate hearing from you.


    oh, btw, this is the second time this function executes when this happens. It uses the same function to parse all server data regardless of the request/response. The first time it works perfectly on both the phone and the emulator. It is the second time that the phone accesses the web that it crashes like this. Dont know if that helps.
    Last edited by skarsten; 2005-02-11 at 00:27.

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    15
    Hello,

    I have ever faced the same problem too...
    The garbage collector seems failed to run automaticaly. To solve this, I try to add System.gc() manualy.

    try {
    ..............
    System.gc();
    }
    catch(Exception e) {
    System.gc();
    }


    hope this can help you ^_^

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    thank you, but that is not the issue.

    I also have this post on the sun forums to see if anyone can help me with it. I got one response so far. Here it is along with my response:

    Re: http and null pointers
    Author: .... Feb 12, 2005 8:52 AM (reply 2 of 3)


    Not sure, but a common error in network programming is relying on read() to read the full amount of the byte array in one call. That is not how it works. It might read the whole amount, but likely you might get back less than you asked for. Check and use the value of the read() result since it tells you exactly how many bytes were read.


    Re: http and null pointers
    Author: .......Feb 12, 2005 2:51 PM (reply 3 of 3)


    Thank you for your response, that is a good point, but that is not the problem.

    I know for two reasons:

    First is that to make sure it was not a data error I tried starting the data printing loop in diferent spots in the array with an offset as well as changing the iterating from front to back to back to front, etc.. Doesnt seem to matter were what section of the array it prints, the data all seems to be there, but it always crashes after about 1200-1300 entries have been printed.

    Second, I have also modified the code to print the amount of data read in (return value from the read() method) as well as the amount that is suppose to come through which is sent in the first two bytes. These values are the same which tells me that it has infact read the entire array after returning from the read() method.

    Because of the first reason, (with the aproxamate number of iterations that it manages to execute) as well as the fact that removing the print loop doesnt prevent the same error on the acutal phone, and the fact that my try/catch blocks dont catch anything, makes me suspect that it is something happining after a certain amout of time, likely in another thread. Now I only have one thread in my client app, BUT the vm probably has more, if for no other reason than that just prior I was accessing the network. It occured to me that maybe I was not releasing all the network resources properly, but after double checking my code, that doesnt apear to be the case which still leaves me stumped.


    Thank you again for responding. Do you have any other ideas?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •