pa7 / nude.js

Nudity detection with JavaScript and HTMLCanvas
http://www.patrick-wied.at/static/nudejs/
2.35k stars 225 forks source link

Web workers never terminated #24

Open joshi1983 opened 4 years ago

joshi1983 commented 4 years ago

A new web worker is made every time scan is called but the old workers are never closed or terminated.

We probably want to call terminate as documented here: https://developer.mozilla.org/en-US/docs/Web/API/Worker/terminate

The following screenshot is from Google Chrome Developer Tools "Source" tab after scanning multiple images with nude.js: image

Related to #1

joshi1983 commented 4 years ago

I added myWorker.terminate() to the onmessage callback to fix the problem for me: ``

   scanImage = function(){
        // get the image data
        var image = ctx.getImageData(0, 0, canvas.width, canvas.height),
        imageData = image.data;

        var myWorker = new Worker('/lib/nudejs/worker.nude.js'),
        message = [imageData, canvas.width, canvas.height];
        myWorker.postMessage(message);
        myWorker.onmessage = function(event){
            resultHandler(event.data);
            myWorker.terminate();
        }
    },

Sorry but I don't have time to clone the repository and create a pull request.