title: PhantomJS crashes after 150-180 urls
body: <p>My script works fine so far, loading every page in the text file line by line in sequentiell order (page.open is asynchron and the page object is global = overwriting on new requests, it's a big clusterfuck running multiple page.open() at once), matching every request for a specific domain and printing JSON values from it.</p>
  
  <p>But if I use a .txt-file with over ~150 links, it just crashes every time, mostly with no error message and with no crash dump like this:</p>
  
  <blockquote>
    <p>PhantomJS has crashed. Please read the crash reporting guide at
    <a href="http://phantomjs.org/crash-reporting.html" rel="nofollow">http://phantomjs.org/crash-reporting.html</a> and file a bug report at
    <a href="https://github.com/ariya/phantomjs/issues/new" rel="nofollow">https://github.com/ariya/phantomjs/issues/new</a>.
    Unfortunately, no crash dump is available.
    (Is %TEMP% (C:\Users\XXX\AppData\Local\Temp) a directory you cannot write?)</p>
  </blockquote>
  
  <p>I can reproduce that easily if I run it multiple times, doesn't matter if I do it at once or one after one.
  How can I prevent the crashes? My script is useless if Phantom can't handle that.</p>
  
  <p>But sometimes I get a crash dump:</p>
  
  <blockquote>
    <p>PhantomJS has crashed. Please read the crash reporting guide at
    <a href="http://phantomjs.org/crash-reporting.html" rel="nofollow">http://phantomjs.org/crash-reporting.html</a> and file a bug report at
    <a href="https://github.com/ariya/phantomjs/issues/new" rel="nofollow">https://github.com/ariya/phantomjs/issues/new</a>.
    Please attach the crash dump file:
    C:\Users\XXX\AppData\Local\Temp\a4fd6af6-1244-44d3-8938-3aabe298c2fa.dmp</p>
  </blockquote>
  
  <p><a href="https://www.dropbox.com/s/i3qi5ed33mbblie/500%20links%20-a4fd6af6-1244-44d3-8938-3aabe298c2fa.dmp?dl=1" rel="nofollow">https://www.dropbox.com/s/i3qi5ed33mbblie/500%20links%20-a4fd6af6-1244-44d3-8938-3aabe298c2fa.dmp?dl=1</a></p>
  
  <p><a href="https://www.dropbox.com/s/najdz9fhdexvav1/500%20links-%2095ebab5c-859b-40e9-936b-84967471779b.dmp?dl=1" rel="nofollow">https://www.dropbox.com/s/najdz9fhdexvav1/500%20links-%2095ebab5c-859b-40e9-936b-84967471779b.dmp?dl=1</a></p>
  
  <p><a href="https://www.dropbox.com/s/1d2t8rtev85yf96/500%20links%20-%20d450c8e1-9728-41c7-ba52-dfef466f0222.dmp?dl=1" rel="nofollow">https://www.dropbox.com/s/1d2t8rtev85yf96/500%20links%20-%20d450c8e1-9728-41c7-ba52-dfef466f0222.dmp?dl=1</a></p>
  
  <p>And in rare cases I even get an error message, Process Explorer says the process has a maximum of 21 threads at once</p>
  
  <blockquote>
    <p>QThread::start: Failed to create thread ()</p>
  </blockquote>
  
  <pre><code>console.log('Hello, world!');
  var fs = require('fs');
  var stream = fs.open('500sitemap.txt', 'r');
  var webPage = require('webpage');
  var i = 1;
  var hasFound = Array();
  var hasonLoadFinished = Array();
  
  function handle_page(line) {
  var page = webPage.create();
  page.settings.loadImages = false;
  page.open(line, function() {});
  
  page.onResourceRequested = function(requestData, request) {
      var match = requestData.url.match(/example.de\/ac/g)
      if (match != null) {
          hasFound[line] = true;
          var targetString = decodeURI(JSON.stringify(requestData.url));
          var klammerauf = targetString.indexOf("{");
          var jsonobjekt = targetString.substr(klammerauf,     (targetString.indexOf("}") - klammerauf) + 1);
          targetJSON = (decodeURIComponent(jsonobjekt));
          var t = JSON.parse(targetJSON);
          console.log(i + "   " + t + "       " + t['id']);
          request.abort;
      } else {
          //hasFound = false;
          return;
      }
  
  };
  page.onLoadFinished = function(status) {    
      if (!hasonLoadFinished[line]) {
          hasonLoadFinished[line] = true;
          if (!hasFound[line]) {
              console.log(i + " :NOT FOUND: " + line);
              console.log("");
          }
          i++;
          setTimeout(page.close, 200);
          nextPage();
      }
  }
  };
  
  function nextPage() {
  var line = stream.readLine();
  if (!line) {
      end = Date.now();
      console.log("");
      console.log(((end - start) / 1000) + " Sekunden");
      phantom.exit(0);
  }
  hasFound[line] = false;
  hasonLoadFinished[line] = false;
  handle_page(line);
  }
  
  start = Date.now();
  nextPage();
  </code></pre>