Memory Management in AngelScript

Started by
2 comments, last by WitchLord 13 years, 8 months ago
Hi,

I have two questions that are related to each other.

Question 1:

Is there a delete operator in AngelScript?

ie:
Sound@ sound = LoadSound("bla1.wav");

// Do something, ie loading level data

delete sound;
@sound = LoadSound("bla2.wav");

// finished
delete sound;


Question 2:
I'm currently using AngelScript to setup and load a level.
The objects are created in AngelScript and the pointers are passed to the Level class. Both Entity and Level are registered with AngelScipt as a reference type.

The problem is that when the script finishes the pointers are deleted by the Release behavior.
But i don't want the objects to be deleted (yet) because they are used by Level.
So i circumvented the problem by commenting the delete line. (see below)

void ReferenceType::Release(){    // Decrease ref count and delete if it reaches 0	if( --mRefCount == 0 )	{        //delete this;	}}


The objects are deleted in the destructor of Level.

// Below is a generic function to register a reference type with AngelScript. The New is used for objects with a default constructor.
template <class T>void New(asIScriptGeneric* gen){	*(T**)gen->GetAddressOfReturnLocation() = new T();}template <class T>void Assign(asIScriptGeneric* gen){	T* a = (T*)gen->GetArgAddress(0);	T* thisPointer = (T*)gen->GetObject();	*thisPointer = *a;	gen->SetReturnAddress(thisPointer);}template <class T>void RegisterReferenceType(asIScriptEngine* engine, const char* type){	int r;	// Register the reference type	r = engine->RegisterObjectType (type, 0, asOBJ_REF);	assert( r >= 0 );	// register reference type behaviours	r = engine->RegisterObjectBehaviour(type, asBEHAVE_ADDREF,  "void f()", asMETHOD(T,AddReference), asCALL_THISCALL);	assert( r >= 0 );	r = engine->RegisterObjectBehaviour(type, asBEHAVE_RELEASE, "void f()", asMETHOD(T,Release), asCALL_THISCALL);	assert( r >= 0 );}


// Below is a generic function to register the Entity type with AngelScript
template <class T>void RegisterEntity(asIScriptEngine* engine, const char* type){	int r;	// Register the reference type	RegisterReferenceType<T>(engine, type);	char declaration[512];	sprintf(declaration, "%s@ f()", type);	// Constructor	r = engine->RegisterObjectBehaviour(type, asBEHAVE_FACTORY, declaration, asFUNCTION(NewEntity<T>), asCALL_GENERIC);	assert( r >= 0 );	// Register = operator	sprintf(declaration , "%s& opAssign(const %s& in)", type, type);	r = engine->RegisterObjectMethod(type, declaration, asFUNCTION(Assign<T>), asCALL_GENERIC);	assert( r >= 0 );}


I call: RegisterEntity<Entity>(scriptEngine, "Entity");
to register the Entity class. Level is registered in the same way.
Advertisement
No, there is no delete operator. When the handle is overwritten, the previous object is released. The object is also released when the handle goes out of scope.

I didn't catch your second question. You have complete freedom to implement the memory management in anyway you want. Your solution to keep the sound in memory until the level ends seems good to me.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Ah oke things are more clear now thanks.
Well what I meant was that I would like to delete objects myself.
Is there some way to declare a "delete" behaviour, just like the "new" behaviour.

// Newr = engine->RegisterObjectBehaviour("Sound@ f()", asBEHAVE_FACTORY, declaration, asFUNCTION(New<T>), asCALL_GENERIC);assert( r >= 0 );// Delete?r = engine->RegisterObjectBehaviour("void delete(Sound@)", asBEHAVE_FACTORY, declaration, asFUNCTION(Delete<T>), asCALL_GENERIC);assert( r >= 0 );


If so, how would the Delete<T> look like?
something like this?
template <class T>void Delete(asIScriptGeneric* gen){	T* a = (T*)gen->GetArgAddress(0);	delete a;}
You can't delete the object before all references to it has been released, otherwise you'll most likely crash the application.

It might be possible to scan all the scripts to find and release all references to your object, but this is something that would take a long time.

I suggest you just make a method to tell the object to free all its internal buffers, making it an empty shell. The scripts that are still referencing the object could have some way of detecting this and releasing it.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement