[SOLVED] - Tracker.autorun memory leak

We are having a problem where the Tracker.autorun function doesn’t properly close after destroying the component, and we are getting multiple executions of this, if we create and destroy the component let’s say 5 times we will get 5 executions.
It isn’t meteor responsability to clear this function?
Is there any way to stop this function manually in the onDestroy hook of the component?
Could be any reason this is not clearing correctly? In the function we are just reading some reactive variables, reading some session info, making some calls to the db and calling a method
Thanks for the help

Pretty solid chance you’re leaking it somewhere - I’ve never seen this where it wasn’t my fault. Can you share some code?

Sure, I’m running this code

Template.addFromTlTemplate.onCreated(function(){
  console.log('creating')
  this.sectionOpen = new ReactiveVar(false);
  this.tradiesData = new ReactiveVar([]);
  this.autoTrigger = new ReactiveVar(1);
  Tracker.autorun((computation) => {
    const autoTrigger = this.autoTrigger.get();
    const job = Jobs.findOne(Session.get('current_id_hash'));
    let currentItemId = Session.get('current_scope_item_id');
    const scope = Scopes.findOne(Session.get('current_scope_id'));
    if(!scope || !job) return;
    let occupation;
    for(let line of scope.lines) {
      if(line._id === currentItemId) {
        occupation = line.occupation
      }
    }
    const occupationObject = Occupation.findOne({name: occupation});
    if(!occupationObject) return;
    let radiusKm = Session.get('radius_value');
    console.log(radiusKm)
    Meteor.call('getPossibleTradies', {latitude: job.address.lat, longitude: job.address.lng, radiusKm, occupationId: occupationObject._id, autoTrigger}, (err, res) => {
      if(err) {
        alertify.error(`Error while getting possible tradies: ${err}`)
      } else {
        this.tradiesData.set(res);
      }
    })
  })
})

That should be this.autorun(.. as that will pass the responsibility of stopping the computation on to the Template instance when the view is destroyed.

3 Likes

That was it, thank you a lot :slight_smile:

1 Like