🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Memory Management: Scripting with operator new and delete overrides

Started by
1 comment, last by justinrwalsh 17 years, 7 months ago
Hello again. I am using angelscript in the Generic form, and am running into an issue with new and delete, along with memory... Now in the graphic's engine I am using, Ogre3D, they override the new and delete operator. I am asuming this is causing an issue with angelscript. I tried to disable the memory manager (new / delete overrides), however i think it is still sneaking in... First off here is my registration methods...

                // Ogre::Vector3
		// Register object type, constructor, destructor and assignment
		r = as->RegisterObjectType("Vector3", sizeof(Vector3), asOBJ_CLASS_CDA); assert(r >= 0);
		r = as->RegisterObjectBehaviour("Vector3", asBEHAVE_CONSTRUCT,  "void f()", asFUNCTION(OgreScriptBindings::Vector3Constructor), asCALL_GENERIC); assert( r >= 0 );
		r = as->RegisterObjectBehaviour("Vector3", asBEHAVE_DESTRUCT,  "void f()", asFUNCTION(OgreScriptBindings::Vector3Destructor), asCALL_GENERIC); assert( r >= 0 );
		r = as->RegisterObjectBehaviour("Vector3", asBEHAVE_ASSIGNMENT, "Vector3 &f(const Vector3 ∈)", asFUNCTION(OgreScriptBindings::Vector3Assignment), asCALL_GENERIC); assert( r >= 0 );
So when i am calling my Construct method this returns a Vector3 but the data types are empty...

void OgreScriptBindings::Vector3Constructor(asIScriptGeneric *gen)
	{
		// Construct the vector3 in memory
		Vector3 *thisPtr = (Vector3 *)gen->GetObject();
		new(thisPtr) Vector3();
		// TODO: Why do i have to do this...?
		thisPtr->x = 0; thisPtr->y = 0; thisPtr->z = 0;
	}
This seams to work in that the x,y,z member variables are are manually initialized. However the constructor should be doing this, right? A call like this...

void OgreScriptBindings::Vector3Constructor(asIScriptGeneric *gen)
	{
		// Construct the vector3 in memory
		Vector3 *thisPtr = (Vector3 *)gen->GetObject();
		//new(thisPtr) Vector3();
		thisPtr = new Vector3();
		// TODO: Why do i have to do this...?
		//thisPtr->x = 0; thisPtr->y = 0; thisPtr->z = 0;
	}
Will initialize the member variables. However once i leave that function and come back to the destructor, the first code segment above retains the values, and the last method does not. And more so, when i go to call a delete the Ogre memory manager kicks back in and causes an error in the overriden delete. I saw a line in the manual that says...
Quote: It is also possible to override how the memory is allocated for an object type, but this will not be described in this article. This can be useful if the object type is allocated from for example a memory pool, or if it is allocated from a DLL which uses another memory heap than the main application.
Is this the solution, to my problems, as i can not seam to find away to diable the memory manager completely in Ogre... I am also going on over to the ogre forums to check and see if anyone else has had issues with scripting engines and the Ogre Memory manager. Thanks Again.
Advertisement
If I recall correctly Ogre overloads the new and delete operators in order to track the memory allocations using Paul Nettle's memory manager.

I recommend that you register the memory functions with AngelScript so that it too will benefit from the memory manager (see manual), though I suspect that it will not solve your problem.

Your problem is not related to the memory allocation itself, because new(thisPtr) Vector3(); doesn't allocate memory, it should only call the constructor using the pre-allocated memory block referred to by thisPtr. It seems that due to some reason the constructor isn't called, probably due to the new() operator not being properly overloaded.

Have you tried setting a break point inside the constructor for Vector3?

It just occurred to me that perhaps you didn't implement the constructor for Vector3. If that is the case, then the explanation is probably that Ogre's memory allocator automatically initializes the memory to 0, whereas the C standard new used as default by AngelScript doesn't. If so, registering the memory functions with AngelScript will indeed solve your problem.

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

Well i checked it out, and I think i would need to write a wrapper function for ogre's memory routines, as none of them match malloc's function signature.

Instead I opted to just disable the memory manager, since in release mode it is turned off anyhow.

As for Vector3, i switched to the constructor Vector3(0, 0, 0). Works like a charm.

Good call on the memory manager.

Thanks Again.

This topic is closed to new replies.

Advertisement