delete of 3dxvector4s

Started by
3 comments, last by Evil Steve 14 years, 6 months ago
hy. I have a function:


D3DXVECTOR4* CSkeleton::updateAnimation(int frame)
{
	D3DXVECTOR4 matrices[MAXBONES*3]; 
	for(int i = 0 ; i < GetBoneCount(); i++)
	{
		CBone* pb = GetBone(i);
		CBone* pbParent = GetBone(pb->m_nIdParent);

		D3DXMATRIX pmxAnimation = pb->getAnimationMatrix(0, 0);
		if(D3DXMatrixIsIdentity(&pmxAnimation))
			pmxAnimation *= pb->getMatrixLocal();

		pb->SetFinalMatrix( pmxAnimation /** pb->getMatrixLocal()*/);
		if (pb->m_nIdParent >= 0)
			pb->SetFinalMatrix(pb->getBoneFinalMatrix() * pbParent->getBoneFinalMatrix());
	
	}
    for(int i = 0 ; i < GetBoneCount(); i++)
	{
		CBone* pb = GetBone(i);
		CBone* pbParent = GetBone(pb->m_nIdParent);
		
		if(pbParent != NULL)
		{
			pb->SetFinalMatrix(pb->getInvBinMatrix() * pb->getBoneFinalMatrix()* pbParent->getBoneFinalMatrix());
			D3DXMATRIX m = pb->getBoneFinalMatrix();		
			matrices[i*3 +0] = D3DXVECTOR4(m._11, m._21,m._31, m._41);
			matrices[i*3 +1] = D3DXVECTOR4(m._12, m._22, m._32, m._42);
			matrices[i*3 +2] = D3DXVECTOR4(m._13, m._23, m._33, m._43);
		}
		else
		{
			pb->SetFinalMatrix( pb->getInvBinMatrix() * pb->getBoneFinalMatrix());
			D3DXMATRIX m = pb->getBoneFinalMatrix();		
			matrices[i*3 +0] = D3DXVECTOR4(m._11, m._21,m._31, m._41);
			matrices[i*3 +1] = D3DXVECTOR4(m._12, m._22, m._32, m._42);
			matrices[i*3 +2] = D3DXVECTOR4(m._13, m._23, m._33, m._43);
		}
	}
	return matrices;
}

that is called by this other function every frame:

	D3DXVECTOR4* matricesNew = m_pSkeleton->updateAnimation(nTime);
	int nCountvfloat4 = sizeof(*matricesNew)/sizeof(*matricesNew[0]);; 
	
	HRESULT hr = m_pMatrices->SetRawValue(matricesNew, 0, nCountvfloat4* sizeof(D3DXVECTOR4));

The problem is that this code generate a lot of memory leack . I try to delete matricesNew but i get an error , how i can delete these resources? Thanks;
Advertisement
Are you sure there's a leak?
If there is, it's not from the code below.
delete what you new, new/delete, alloc/dealloc, etc..

Side question; Why allocate n=MAXBONES vectors, but assign them using n=GetBoneCount()? Why run the risk of GetBoneCount()>MAXBONES?
i'm try to comment the function:
D3DXVECTOR4* CSkeleton::updateAnimation(int frame) that i have posted and the leak disappear, then is probably that this is the bad function.
Thanks
I'm not completely sure about this, but what your function is returning will be a pointer pointing to garbage. The matrices[] variable will only exist inside your updateAnimations() function, and once the function ends, it will be removed.

And as marius1930 said, you shouldn't be using delete as you haven't allocated any memory on the heap (using new).

You could consider either returning a vector for example:
std::vector<D3DXVECTOR4> CSkeleton::updateAnimation(int frame){    std::vector<D3DXVECTOR4> matrices (MAXBONES * 3);    // update animation.    return matrices;}

Or you could pass in a pointer to a D3DXVECTOR4 array as a parameter:
std::vector<D3DXVECTOR4> CSkeleton::updateAnimation(int frame, D3DXVECTOR4* matrices ){    // update animation using matrices array.}
The code you have there doesn't return valid data - it's returning the address of a local variable; you can't return an array from a function like that. I'd be very surprised if your compiler isn't warning you about that error (Make sure your warning level is set to maximum). You should do what Xycaleth said, and return the array wrapped in another structure.

So long as GetBone() doesn't return a bone that needs deleted or released in some way, there's no memory leaks there either.

This topic is closed to new replies.

Advertisement