Search Unity

Random crashes when using WWW class

Discussion in 'PSM' started by madgreg, Sep 9, 2014.

  1. madgreg

    madgreg

    Joined:
    Aug 29, 2013
    Posts:
    35
    I use a lot WWW class for http request with my server and it seems it causes the game to terminate itself sometimes. My game is rejected because of that and I don't know what is wrong cause this code works very well on any other platforms, iOS, android and windows phone. There must be something in http request handling that causes the crash but the console output isn't really helpfull, I got things like this when it crashes:


    [21.854279] HTTP status Code is 200
    [21.854792] HTTP start transfer of unknown size
    [21.864551] HTTP end transfer
    [21.867631] HTTP Done downloading
    [21.868067] HTTP Done downloading
    [21.868465] HTTP Done downloading
    [21.869813] HTTP Done downloading
    [21.870241] HTTP Done downloading


    I don't know with I got 5 times "HTTP Done downloading" for just 1 request...


    I'm stuck here :( Any ideas?
     
  2. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    377
    Can you post the code snippet/script ?
     
  3. madgreg

    madgreg

    Joined:
    Aug 29, 2013
    Posts:
    35
    Yes :


    Code (CSharp):
    1. public class Webrequest : MonoBehaviour
    2. {
    3.     private static string baseurl = "http://live.latest.something.appspot.com/";
    4.     private static string betaurl = "http://dev.latest.something.appspot.com/";
    5.  
    6.     public delegate void RequestResponseDelegate(RequestStatus s, string response, object dico);
    7.     protected RequestResponseDelegate dlg = null;
    8.  
    9.     public enum RequestStatus
    10.     {
    11.         RequestFailed,
    12.         RequestSucceeded
    13.     }
    14.  
    15.     public Webrequest()
    16.     {
    17.     }
    18.  
    19.     public void SendJsonRequest(string action, string parameters, RequestResponseDelegate d)
    20.     {
    21.         dlg = d;
    22.         string url = (Data.distribution ? baseurl : (Data.betaserver ? betaurl : baseurl)) + action;
    23.  
    24.         WWWForm form = new WWWForm();
    25.         form.AddField(@"data", parameters);
    26.         WWW www = new WWW(url, form);
    27.  
    28.         if (!Data.distribution)
    29.             ConsoleLog.Log("Calling " + url + " with Params: " + parameters);
    30.  
    31.         StartCoroutine(WaitForPostRequest(www));
    32.     }
    33.    
    34.     protected IEnumerator WaitForPostRequest(WWW www)
    35.     {
    36.         yield return www;
    37.  
    38.         // check for errors
    39.         if (www.error == null)
    40.         {
    41.             /*
    42.             if (!Data.distribution)
    43.                 ConsoleLog.Log("WWW Ok!: " + www.text);
    44.             */
    45.  
    46.             if (dlg != null)
    47.             {
    48.                 dlg(RequestStatus.RequestSucceeded, www.text, www.text.dictionaryFromJson());
    49.             }
    50.         }
    51.         else
    52.         {
    53.             ConsoleLog.Log("WWW Error: " + www.error + " (" + www.url + ")");
    54.             if (dlg != null)
    55.                 dlg(RequestStatus.RequestFailed, www.error, null);
    56.         }
    57.        
    58.         EventManager.Instance.InvokeNextFrame( () =>
    59.         {
    60.             StopCoroutine("WaitForPostRequest");
    61.             UnityEngine.GameObject.Destroy(this);
    62.         });
    63.     }
    64. }
     
  4. PeteD

    PeteD

    Joined:
    Jul 30, 2013
    Posts:
    71
    From what I can see the StopCoroutine will not work as this only works for Co routines STARTED with a string i.e. you would have needed to start your co routine with this StartCoroutine("WaitForPostRequest");

    I'm also not sure what stopping a co-routine with a call from within that co-routine will do as I've never tried that.
     
  5. madgreg

    madgreg

    Joined:
    Aug 29, 2013
    Posts:
    35
    Thanks for this I don't know how I could miss that! So now I start the coroutine with StartCoroutine("WaitForPostRequest", www);
    It seems it crash less often, but still crashes a while afterwards...
     
  6. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    377
    If it crashes less often now.. is it something else in your project that is also contributing towards crashing? Whats the console outputs say now.
     
  7. madgreg

    madgreg

    Joined:
    Aug 29, 2013
    Posts:
    35
  8. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    377
    You have so many plugins and scripts in that test project >_< have you tried that one google analytics script (i presume its that one your on about) on a completely blank project, no other scripts or plugins etc.
     
  9. madgreg

    madgreg

    Joined:
    Aug 29, 2013
    Posts:
    35
    hmm no, you think it comes from that plugin? I know I have many plugins, it's hard to strip them off the project
     
  10. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    377
    Make a blank project from scratch, and add that one google analytics script/s.
    I know a lot of plugins don't work with PSM (at the moment) so i do think its one of them :)
     
  11. PeteD

    PeteD

    Joined:
    Jul 30, 2013
    Posts:
    71
    The version of Google Analytics doesn't seem to be compatible with 4.3 as it's failing to even build under it. The error seems to be that WWW can't convert param 3 i.e. the header from a dictionary<string, string> to a hashtable.

    Where did you get this version of google analytics. Have you checked that it doesn't require unity 4.5 or something like that.
     
  12. madgreg

    madgreg

    Joined:
    Aug 29, 2013
    Posts:
    35
    It's building fine with last version of Unity PSM. I think I made a #define to switch easily between dictionary and hashtable.
    Anyway I tested after removing this plugin and it crashes the same, I think the problem comes from everything that is using WWW class. I use it a lot even without this google analytics problem. I don't know why it crashes uniquely on the ps vita... It should'nt because it's working fine on every other platform.
     
  13. PeterD

    PeterD

    Joined:
    Feb 6, 2013
    Posts:
    120
    I've done some testing at my end and it looks like it's the Unity WWW class that is the issue. I can usually download just fine but, get weird errors from time to time almost as if I'm clashing with some process of the vita OS. Have you raised a bug with the unity guys? I suspect they need to make some changes to get WWW working consistently on a Vita.
     
  14. madgreg

    madgreg

    Joined:
    Aug 29, 2013
    Posts:
    35
    Yeah I think there is a problem with WWW class on vita too... I sent a message to a PSM Dev Support on playstation forums but no news yet. How can I raise this bug to unity guys?
     
  15. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
  16. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    @madgreg
    Turns out this is not at all related to your webrequests.
    Instead it's a very old bug in the Dispose method of MD5CryptoServiceProvider (actually still present in the latest Mono corlibs from what I can see). Creating multiple MD5CryptoServiceProvider objects (more than 5-10) before the GC kicks in causes a crash in the GC finalizer.

    The problem is resolved now, and the next release will include the fix. Currently the only workaround is to not use MD5CryptoServiceProvider (or at least make sure the object is not destroyed)..
     
    PeteD likes this.
  17. PeteD

    PeteD

    Joined:
    Jul 30, 2013
    Posts:
    71
    Um I don't remember ever even creating one let alone storing a reference to it. Am I missing something?
     
  18. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    I was referring to @madgreg 's repro project for the WWW crash... ?