Sign in to follow this  
treeway

Deleting Matricies and Vectors

Recommended Posts

Hi Since a D3DXVECTOR3 and a D3DXMATRIX are structs contianing only floats is it necissary to delete them? if so, how do you do it as ive found that using delete or SafeDelete() (which just checks if the pointer is NULL first) dont work. thanks

Share this post


Link to post
Share on other sites
Same way as you delete any other struct or class?

If it's been new'ed, you delete it. If it is allocated on the stack, you just let it go out of scope.

Share this post


Link to post
Share on other sites
If you have allocated them with new, then you need to deallocate them with delete, if you have just created them on the stack you shouldn't delete them:
Correct:

D3DXVECTOR3 a;
D3DXVECTOR3 *b = new D3DXVECTOR3;
D3DXMATRIX c;
D3DXMATRIX *d = new D3DXMATRIX;
delete b;// b is allocated on the heap and should be deleted.
delete d;// d is allocated on the heap and should be deleted.


Wrong:

D3DXVECTOR3 a;
D3DXVECTOR3 *b = new D3DXVECTOR3;
D3DXMATRIX c;
D3DXMATRIX *d = new D3DXMATRIX;
delete a;// Should give an error since you can't delete stack objects
delete c;// Should give an error since you can't delete stack objects
// Memory leak, b and d is never freed.


This is the same for all other objects if they are allocated on the stack the destructor automatically takes care of everything. If they are allocated on the heap you have to deallocate them, no matter whether they are ints or complex ADTs. DirectX uses some technology known as COM, when you accquire a pointer from COM you should delete it with delete, you should instead "release" it as you most likely have tried, this only works with technology built upon COM though.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this