Deleting Matricies and Vectors

Started by
2 comments, last by treeway 17 years, 6 months ago
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
Advertisement
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.
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 objectsdelete 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.
okay thanks guys

This topic is closed to new replies.

Advertisement