Search Unity

WWW Class load causes crash

Discussion in 'Scripting' started by RayX, May 27, 2009.

  1. RayX

    RayX

    Joined:
    May 27, 2009
    Posts:
    6
    Hi,

    I have to load lots (approx. 30.000 or more) external PNG images into my app during runtime :eek: (load time doesn't really matter, since I only grab single frames).
    I tried this using the WWW class, but after nearly 6000 WWW-load cycles my app crashes. I think this is no memory problem, because resizing the images doesn't change a thing.

    Is there any other way to load external images? (from local disk into Standalone-App)

    I hope somebody can help ...
     
  2. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,355
    Why do you have to load so many PNGs?

    Very likely memory is increasing--you should destroy the textures after you're done processing them.
     
  3. RayX

    RayX

    Joined:
    May 27, 2009
    Posts:
    6
    I have to render an animation with 10 different movie-sequences.
    the movies stored as single frames to have access to every frame and to get the movies in sync with the animations.

    i have a script that loads all the textures for one frame, then it assigns the loaded textues to the relevant objects. then it calls another script to save a screenshot, frame by frame, no realtime needed.

    all functions called inside FixedUpdate, and to have enough time to load all images i set fixedDeltaTime and timeScale according to the speed of my mac.

    hmmm ... i've watched the memory usage of my app, it goes up and down, and it uses approx 1 GB of memory, no more. (the memory-increasing-thing was the first problem - but i think i fixed this already)

    ... :cry: :cry:

    my loading code:

    Code (csharp):
    1.         var frame = new WWW (moviePath+frameNum+".png");
    2.         yield frame;
    3.         frame.LoadImageIntoTexture(MovieObjects[i].renderer.material.mainTexture);
    4.  
    this works with 10 textures per frame until frame # 597 (of 3000), after 5975 (or so) times the WWW Class was called the app crashes (even in the editor).
    it also crashes when i remove the "LoadImageIntoTexture"-line, so that the images are not applied to anything.
    It don't crashes with the "WWW" line removed!

    what could i do?
     
  4. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,355
    What part needs to be interactive?

    I'm just wondering why you aren't doing this as a preprocess step (as some Perl/Python/whatever script)...
     
  5. RayX

    RayX

    Joined:
    May 27, 2009
    Posts:
    6
    this is not really interactive, but the images needs to be arranged on 3d-models of old tv sets. i have another script, that controls which movie is shown on which tv at which frame, and there are more than 200 tv's in this animation (see image). every tvscreen needs different tint-colors and many more schnickschnack - that can be best controlled via scripting ...
     
  6. Almar

    Almar

    Joined:
    May 11, 2009
    Posts:
    87
    Hm, and you cannot use the movie classes instead? (ScriptReference/WWW-movie.html)
     
  7. RayX

    RayX

    Joined:
    May 27, 2009
    Posts:
    6
    YEAH!
    I broke the magical 5970 load-cycles-barrier!


    :D :D :D

    I 'm using System.IO.FileStream instead of WWW

    (i cannot use MovieTextures, because there is no possibility to access single frames of the movie at any time)

    here's the code:

    Code (csharp):
    1.  
    2. // this is the "original" code that crashes the app after 5970 calls
    3.  
    4. function LoadImageFiles() {
    5.     var Image = WWW("yourpath/yourfile.png");
    6.     yield Image;
    7.     Image.LoadImageIntoTexture(renderer.material.mainTexture);
    8. }
    9.  
    10.  
    11. // and here's my replacement, without using WWW
    12.  
    13. import System.IO;
    14.  
    15. function LoadImageFiles() {
    16.     var fs = new FileStream("yourpath/yourfile.png", FileMode.Open);
    17.     var ImageBytes = new System.Byte[fs.Length];
    18.     fs.Read(ImageBytes, 0, fs.Length);
    19.     fs.Close();
    20.     renderer.material.mainTexture.LoadImage(ImageBytes);
    21. }
    22.  
    Meanwhile my testapp has loaded more than 250.000 Images and it's still running - 10 images per frame, 10 frames per second - seems to be not much slower than the WWW class :)
    With the code above memory usage is stable too - no increasing!

    However, i do not know whats the problem with the WWW class ...
     
  8. Almar

    Almar

    Joined:
    May 11, 2009
    Posts:
    87
    Nice! Maybe you could put it on the wiki as well!
     
  9. MrMetwurst2

    MrMetwurst2

    Joined:
    Jul 16, 2009
    Posts:
    245
    Did you try Destroying the WWW object and/or the Image after it's been used? That should fix your memory leak problem.

    Thanks heaps for the FileStream code though. That has been super useful for an app I'm making :)