# A question about freeing resources in on('close')

**URL:** <https://discourse.nodered.org/t/a-question-about-freeing-resources-in-on-close/29450>\
**Category:** Developing Nodes\
**Created:** [3 July 2020 15:49 UTC](https://discourse.nodered.org/t/a-question-about-freeing-resources-in-on-close/29450 "2020-07-03T15:49:38Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 July 2020 15:49 UTC](https://discourse.nodered.org/t/a-question-about-freeing-resources-in-on-close/29450/1 "2020-07-03T15:49:38Z")

</div>

I am developing a node that will spawn a process which may run for minutes, so in `on('close')` I should kill the spawned process. The complicating factor is that if the node is sent multiple messages it will spawn a process for each message, therefore I have declared the variables relating to each process (pid etc) as local variables in `on('input')`. The result is that I don't have access to those variables in the close function. Is there some neat trick I can use to get over this or do I need to do something like storing them in an array at the node level and keeping the index in the input function. I can see that getting a bit involved to manage.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [3 July 2020 16:16 UTC](https://discourse.nodered.org/t/a-question-about-freeing-resources-in-on-close/29450/2 "2020-07-03T16:16:16Z")

</div>

Well you need to make sure you do have access to them :-). You could save them in a variable that is global (within your node) or attach it as a property to the node. Eg node.mypids. Etc

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 July 2020 16:44 UTC](https://discourse.nodered.org/t/a-question-about-freeing-resources-in-on-close/29450/3 "2020-07-03T16:44:23Z")

</div>

> [@dceejay](#):
>
> Eg node.mypids.

Yes, that is what I was thinking of, but I can't work out the best way to organise the data. I have only recently started using Javascript. For example, if I use an array and push the new ones on the end and set old ones to null (or something) the indexes will get bigger and bigger over time, but does the fact that js uses sparse arrays mean that doesn't matter as the ones I null out won't actually exist any more. Or perhaps there is a better way. In C++, for example, I would use a linked list of objects.

**[Edit]** OK, I see the Exec node does a similar thing, I am looking at how it does it....

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [3 July 2020 17:12 UTC](https://discourse.nodered.org/t/a-question-about-freeing-resources-in-on-close/29450/4 "2020-07-03T17:12:10Z")

</div>

I'd possibly use an object & dynamic properties. E.g.

```auto
//Create a property using pid as Property name
node.pids[pid] = process;//store the child process obj as a property keyed by pid 

```

Then you can easily look up a child process...

```auto
var proc = node.pids[msg.pid];

```

and when it's finished with, you can delete it...

```auto
delete node.pids[pid];

```

Other tips....

```auto
//Get & kill all processes
var pids = Object.keys(node.pids);
for(var p in pids){
  var child = node.pids[p];
  child.kill();
}

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [3 July 2020 17:13 UTC](https://discourse.nodered.org/t/a-question-about-freeing-resources-in-on-close/29450/5 "2020-07-03T17:13:56Z")

</div>

Yes just use an object not an array, with a suitable key. Maybe “P”+ pid or something easy (but javascript friendly). As you will want to also remove them when they complete normally.

---

<div class="post-metadata">

**Author:** ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)\
**Post date:** [3 July 2020 17:29 UTC](https://discourse.nodered.org/t/a-question-about-freeing-resources-in-on-close/29450/6 "2020-07-03T17:29:17Z")

</div>

Javascript objects need strings as keys (I know, you can also use integers).  
If you don't want to invent a string key for storing that stuff, you can have a look at  
`new Map()` or `new Set()`, which can take arbitrary objects as "keys" (i.e. you could save the process directly in the set) and allows nice iteration without having to worry about `hasOwnProperty` or using `Object.keys()`, and it has functions for deleting etc. (If you are a c++ guy, they are pretty similar to some of the std containers)

Just saying, the method with the standard object Steve and Dave posted are of course also valid.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 July 2020 17:35 UTC](https://discourse.nodered.org/t/a-question-about-freeing-resources-in-on-close/29450/7 "2020-07-03T17:35:35Z")

</div>

Yes, that is how the exec node does it, obvious when you know how, as are many things.  
Thanks all.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [3 July 2020 17:37 UTC](https://discourse.nodered.org/t/a-question-about-freeing-resources-in-on-close/29450/8 "2020-07-03T17:37:16Z")

</div>

> [@Colin](#):
>
> that is how the exec node does it

Does it? that was of the top of my head, on my mobile, 3 beers in. Great minds must think alike 😉

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [3 July 2020 18:33 UTC](https://discourse.nodered.org/t/a-question-about-freeing-resources-in-on-close/29450/9 "2020-07-03T18:33:19Z")

</div>

...Fools seldom differ ... 😜

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [1 September 2020 18:33 UTC](https://discourse.nodered.org/t/a-question-about-freeing-resources-in-on-close/29450/10 "2020-09-01T18:33:22Z")

</div>

Closed on 1 September 2020 18:33 UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
