# Scripting garbage collection and destructor

This topic is 2104 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been thinking of using Squirrel as a scripting language.

My idea was to use the garbage collector as a memory manager:(squirrel as an example script)

class Model
{
constructor(filename)
{
}

destructor()
{
}

model_id = null;
}


Something like this.(load_model and unload_model is native functions that are really easy to bind no need to worry about binding complicated classes)

It's obvious that this isn't going to work. Because Squirrel is missing the destructor.

Am I thinking of something wrong?

Is there a reason why Squirrel doesn't have a destructor?

Am I just using scripting in a wrong way?

Are there any alternatives to Squirrel that is similar to it but with a destructor?

Should I rethink the problem with binding to the native functions?

##### Share on other sites

You are right it has really opened my eye to something also it's really nice and cache friendly to store models in that way.

The problem is that isn't scripting languages one the reasons of using a scripting language is to use the garbage collector.

Hmmmm...

Clean up after a level is loaded shouldn't be a problem when all the allocations are controlled by the engine itself. It certainly is faster to deallocate chunks of memory also more cache friendly.(almost feels like my C++ code becomes more C like)

The thing is that I was thinking that scripting should be more a part of the engine rather than just a slave to the engine.

##### Share on other sites

As a side note, GC collected languages generally have finalizers instead of destructors. They're almost the same thing but with an important difference: dtor is called deterministically while finalizer is called "at some point in time" immediately before object is cleared.

This gives an important implication: most of the time you need to "clear" (in gameplay terms) an object as a response to an action.

I've witnessed a dude cracking his head open at a bug originating exactly from this behaviour: he expected an object to go away immediately after dropping its reference. This of course wasn't the case and since he tied existence to visualization, some sprites appeared to be all over the place.

So, maybe this is not completely related to your needs but I hope it helps you thinking.

• 18
• 29
• 11
• 23
• 16