Heap Space Issue

edited May 2016 in Library Questions

I wrote a sketch to pair random strings from a text file with different clips. The program will run for a while but eventually crash due to there not being enough heap space. I don't understand how this is happening if I am resetting 'myMovie' and 'p' at the end of draw(). Any help would be greatly appreciated!

import processing.video.*;

PFont font;

String[] posts; // strings loaded in setup()
String[] videos = {"1a.mov", "2a.mov", "3a.mov", "4a.mov", "5a.mov", "6a.mov",
                    "7a.mov", "8a.mov", "9a.mov"}; // video clips

String post;
Post first; // First post
Post p; // Next iteration of posts

Movie myMovie;
String clip;
int count; // Iteration counter

int a = 0; // image()
float duration = 0; // Movie duration
float time = 0; // Movie time

void setup(){

  size(displayWidth, displayHeight);
  background(0);

  posts = loadStrings("posts.txt"); // load strings from file
  font = loadFont("HelveticaNeue-Bold-48.vlw"); // load font
  post = posts[int(random(posts.length))]; // use random post

  textFont(font); // Set text font 
  textSize(50);
  textAlign(CENTER);
  fill(255, 248, 43); // Yellow fill

  if (frame != null){
    frame.setResizable(true); // resizable window
  }  

  /** Random generation of initial clip and post */
  clip = videos[int(random(videos.length))];
  myMovie = new Movie(this, clip);
  makeTint();
  myMovie.play();
  count++;
  first = new Post(post);
  println("Playing clip: " + clip + " w/ post: " + "\"" + post + "\"");
  println("Iteration: " + count + "\n");
}

/** Will generate random clips and posts after initial clip **/
void draw(){

  if (a == 0){
    image(myMovie, 0, 0);
  }

  image(myMovie, 0, 0);

  duration = myMovie.duration();
  time = myMovie.time();

  /** If clip is at end **/
  if ((duration - time) < 0.1){

    first = null; // Remove first post

    /** Reset clip **/
    clip = null;
    myMovie = null;
    clip = videos[int(random(videos.length))];
    myMovie = new Movie(this, clip);
    count++;

    makeTint();
    myMovie.play();

    /** Reset post **/
    p = null;
    post = posts[int(random(posts.length))];

    println("Playing clip: " + clip + " w/ post: " + "\"" + post + "\"");
    println("Post length: " + post.length());
    println("Iteration: " + count + "\n");

  }
    p = new Post(post);
}

/** Method needed to play clips **/
void movieEvent(Movie m){  
  m.read();  
}

/** Screenshot taken and saved if user mouse-clicks **/
void mousePressed(){  
  int random_number = int(random(1000000));
  save(random_number+".png");  
}

/*******************************************\ 
* Function: Create Tint of random color
*
* Parameter Description:
*-----------------------------------------
* none  
\*******************************************/
void makeTint(){
  tint(int(random(200, 255)), int(random(200, 255)), 
        int(random(200, 255)), 126);
}


/** Class for displaying post **/
class Post{

  /*******************************************\ 
   * Function: Post Object Constructor
   *
   * Parameter Description:
   *-----------------------------------------
   * t, text to display
  \*******************************************/
  Post(String t){
    text(t, width/2, height - 150);    
  }

}

Answers

Sign In or Register to comment.