GUI Container: After the dispose is executed, the container’s children are not cleaned up

After the dispose is executed, the container’s children are not cleaned up. :frowning:
Or there are some errors in my code;
I just want to call dispose in a container to destroy all the data.
There is a playground:
https://playground.babylonjs.com/#9AJQWX

1 Like

Interesting bug. When I look at that I don’t see a bug, because from my perspective when dispose() is called on an object I have no intentions of using it again, so it’s current state is not so significant. Dispose pattern is used normally used for releasing resources, not updating component structure. I would think you should be removing any references to the container and allowing the garbage collector to ‘clean up’ after dispose(). I’m looking forward to hearing other responses for correct/intended usage! :slight_smile:

1 Like

Hi guys. According to the note in the playground, 6W says it is caused by container.dispose - line 417, right?

Because of the way that “of” works, and because each control in container._children… removes ITSELF from the ._children array (done during its control.dispose() call), then only children[0], [2], [4], [6], [8] etc… gets control.dispose() called on them.

Container._children[1], [3], [5], [7], etc… never get their control.dispose() called.

If lines 417-419 was changed to: while (this._children[0]) { this._children[0].dispose() } … would that fix the issue? No longer would “of” get confused every time a control removes itself from a container._children array.

Just thinkin’. 6W did a lot of good legwork… to compile his/her “reason” in lines 102-117 of the playground. Every time a control.dispose() is called, container._children becomes smaller by one… which is screwing-up the “of” directive… causing it to skip every-other control.

I think… the WHILE method… will eliminate the problem. But, I don’t think overly well, so I could be wrong. :slight_smile:

1 Like

Hi, @WWWWWW , @brianzinn is correct,
Disposed objects should not be accessed.
Just remove any references and let GC do it’s job in peace :slight_smile:

parentContainer.dispose();
parentContainer = null;

Please forgive me for being a bit sloppy. I just think that when I call dispose, it means that I am destroying all the data. But this is not the case, which makes me confused.:joy:
Thank you very much for your answer again. @aWeirdo @brianzinn

’ last in,first out ', Maybe better? So I will create a loop from [n] to [0] instead of [0] to [n]. Another reason is that every time it will cause an array operation, when I remove [0].:slight_smile:

1 Like

Yeah, that would work, 6W. But, long-term, I think we will want to fix this correctly, and avoid work-arounds and compromises.

https://playground.babylonjs.com/#9AJQWX#3

Watch console (when pressing dispose button).

control dispose(): I am 0
control dispose(): I am 2
control dispose(): I am 4
control dispose(): I am 6

(make sure you have a VERY fresh LOAD of the playground… before testing)

No dispose() calls to 1,3,5,7… as the symptom indicates.

In lines 2-32, I over-ride the default control.dispose()… inserting a console.log into it.

Your bug report was perfect, your playground was perfect, and your reason was spot-on correct (in my opinion). Good work for your first post on your first forum day. Well done!

Here’s another PG: https://playground.babylonjs.com/#9AJQWX#7

Again, watch console during dispose button press. Notice lines 35-38, container.dispose() over-ride, using the WHILE method.

All 8 children… disposed. Original container still exists, because line 36 is failing… no such thing as super.

Definitely a bug! Will fix it for next commit!

Thanks a lot guys

1 Like

Thanks Wingy for following up properly! Nice catch :slight_smile:

1 Like

Thx BZ.

Dubya dubya dubya dubya dubya dubya did all the work. I just noticed the pretty PG pattern of odds and evens, and I got fascinated. :slight_smile: First time I’ve ever seen the “of” thing, too.

What WAS 417 before it was repaired?

for (var control of this._children) { spank brutally }

Sort of like…
for (var control in this._children) {...}

Of. In. Hmm. Other than being completely different, they are identical. (huh?)

of iterates through the content
in iterates through the indices

ahhhh, thx DK! interesting!

“of” saves one line of code, OR saves repeated use of this._children[control]. Yep yep yep. “of” rocks.

Let’s see… a fun one might be: for (amber_waves of grain) { console.log(spacious_skies) }

Or maybe… for (corners of the_Earth) { ... } ? :slight_smile: Goofy.

for (the_sake of all_that_is_holy) {} ? heh

for (the_love of corn_nuts)

(sorry, mental drift)

1 Like

Wingnut - you may be interested in the rockstar programming language. I’m sure you could write some sweet
lyrics/programs in it: GitHub - dylanbeattie/rockstar: The Rockstar programming language specification

2 Likes

Thank you very much for your help, everyone. I am so fortunate.:smiley:
I feel a little pity that I didn’t get involved in the discussion in time.
Always looking forward to a better ‘BABYLON’.

2 Likes