Hi all, I have a quick question: I have a class (Class A) that creates three sprite objects through another class (Class
.
How should I approach memory cleaning when I instance Class A in my game loop but then delete all of the children of Class A manually?
Comments
item.lua
mechanic.lua
Here is the way I understand it
Let's look at this hierarchy:Rectangles representing objects and arrows representing reference.
As you may know, when object has no references it can be garbage collected.
So let's see:
If you remove gameStage from stage, then gameStage does not have any references left to it, so it will be garbage collected.
So next step, you nil out the myTech variable
It means there are no references to Trio instance, so it will also be garbage collected.
Which means that there are no references left to all Bello instances, so they also will be garbage collected.
Same goes to bitmap. We had a local variable inside Bello class and we also added same bitmap as a child of Bello instance. You could think that it's two references, but, when Bello instance is garbage collected, local variable bitmap will also be removed automatically.
That is why it's important to use local variables and not global ones, unless you know what you're doing
So all that is left is a Bitmap instance without parent (Bello was removed), which means there are no references to it, which means it will also be garbage collected
So basically minimum what you need to do is to remove gameStage from stage and nil out the myTech variable
Hope that clears something up
And please correct me if I'm wrong
P.S hopefully that will make a nice post for my 600th forum post
Likes: gianmichele