Jump to content
  • Advertisement
Sign in to follow this  
giugio

delete of 3dxvector4s

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!