API Question DelAnimation and dynamic MGROUP_TRANSFORMs

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
11,318
Reaction score
2,786
Points
203
Location
between the planets
The tutorial in the API guide does not use DelAnimation, but it states that you have to delete dynamically allocated MGROUP_TRANSFORMs on exit. My question now is, does DelAnimation do this for all its transforms? Also, the the API docummentation states that DelAnimationComponent does not yet work for components that has children, so I'm wondering if I should use DelAnimation at all or just manually delete all the transforms?

If so, what about the components? I only have a handle to them, so I can't delete them, but then again, the tutorial never talks abot deleting components. Are those deallocated automatically on vessel destruction?
 
DelAnimation(UINT anim) deletes the animation using the animation ID. It does not delete the memory allocated for the mesh group transformation structure MGROUP_*

So bad news buddy, you 'll have to take care of deleting it yourself :P

Here is some code :

Code:
// Animation setup
UINT group[VesselCollisionDef::MAX_SHAPES][1]; //  participating groups

// mesh group transforms
MGROUP_SCALE *mgsSX[VesselCollisionDef::MAX_SHAPES];

//Animation IDs
UINT animSX[VesselCollisionDef::MAX_SHAPES];
	
// Animation handles
ANIMATIONCOMPONENT_HANDLE firstAnimHandle, parent;

// Mesh indices
UINT mMeshIndices[VesselCollisionDef::MAX_SHAPES];


/**
 * Create the scaling animations
 */
int
Bump_MshCollisionShapeAttached::createScalingAnimations(float sx, float sy, float sz)
{
	group[mNumShapes][0] = 0;

	mgsSX[mNumShapes] =  new MGROUP_SCALE( mMeshIndices[mNumShapes], group[mNumShapes], 0, _V(0, 0, 0), _V(sx, 1, 1));
	animSX[mNumShapes] = vMesh->CreateAnimation (0);
	parent = vMesh->AddAnimationComponent (animSX[mNumShapes], 0, 1, mgsSX[mNumShapes]);

	.......

	return 0;
}

So here I needed to manually free mgsSX[] using delete[] mgsSX
 
Last edited:
If you allocated `some_pointer = new MGROUP_TRANSFORM (...)`, DelAnimation won't deallocate the content of `some_pointer` for you, because you could also allocate it on stack or in the data segment, which you don't deallocate manually, or by using a different allocation method (VirtualAlloc for example). For every `new`, which returns an address, you should have a `delete` to which you pass that address.
 
off topic point : I have this friend of mine who works on gas turbines and they do loads of stuff on thermodynamics cycles and stuff. He tells me that its all about accounting the energy in each step of the cycle. Whats taken, must be given back by the law of conservation of energy.

Well we have conservation of memory here :)
 
So here I needed to manually free mgsSX[] using delete[] mgsSX

also kind of off-topic, but shouldn't just delete mgsSX be enough in this case? After all, the array does have a fixed size on allocation...
 
Code:
// Clear the Meshgroup scaling transformations
	for (int i = 0; i < mNumShapes; ++i) {

		oapiWriteLogV("Bump_MshCollisionShapeAttached::removeShape : Deleting %p, %p, %p",
				mgsSX[i], mgsSY[i], mgsSZ[i]);


		delete mgsSX[i];
		delete mgsSY[i];
		delete mgsSZ[i];
	}

Well yeah that code was not exactly what I do. Above is what I do. Every element in the array is a pointer to a dynamically allocated memory location, so each needs to be freed in turn. If I had allocated something like this :

int *pMyArray = new int[20];

then I could

delete[] pMyArray

so all the sizeof(int) * 20 bytes of memory is returned to the system.
 
Last edited:
Back
Top