Dynamic arrays and memory leaks
hI,
Basically, my "load file" function executes fine but when I come to render just one triangle, the XP illegal operation message comes up. I ran the utility in the "Utils" folder of the SDK and it says there are memory leaks. I checked the lines it mentions and the leaks are all on dynamic array lines.
I''ve got quite a few dynamic arrays in the code. One for :
Vertices
Textures
Alpha Textures
Primitive Counts
Texture numbers //The number applies to the indes number of a texture in the Texture array.
Indices
An example of how I''m writing dynamic arrays is:
//read in Totalndices
int *indices;
indices = new int[TotalIndices];
Is there anything wrong? Also, if TotalIndices was 10, would the array indexes go from 0 - 9, 1 - 10 or 0-10?
Thanks
de_matt
On a side issue is it allright to use a Vertices Class? The vertices class includes Position, Normal, Diffuse, Specular, Textures 1-8 and a constructor and a deconstructor. Will the constructors interfere?
Thanks
It will go from 0-9. Of course your using delete [] to free the arrays, correct?
As far as your class, it's OK for use with Direct3D as long as each item in the class gets put in the same position and offset as it would in a struct (if I understand you, your question isn't that clear.) Of course keep in mind that in C++ all structs are just classes anyways....
Edit: DirectX has a #define D3D_OVERLOADS which does some extra stuff with the D3D_VECTOR structure (heck it's now got constructors) so it wouldn't be the first time that somebody did it if you modify your vertex structure.
[edited by - UberGeek on November 6, 2003 11:09:41 AM]
As far as your class, it's OK for use with Direct3D as long as each item in the class gets put in the same position and offset as it would in a struct (if I understand you, your question isn't that clear.) Of course keep in mind that in C++ all structs are just classes anyways....
Edit: DirectX has a #define D3D_OVERLOADS which does some extra stuff with the D3D_VECTOR structure (heck it's now got constructors) so it wouldn't be the first time that somebody did it if you modify your vertex structure.
[edited by - UberGeek on November 6, 2003 11:09:41 AM]
I wasn't using delete but I realised the error of my ways. Is this right then
int *bob;
bob = new int[10];
delete bob;
I'm doing that and it works find for all my arrays except my vertex class array. When I try it with that I get an assertion message with "abort, retry, ignore". What could be causing this?
On the vertices class bit, would it be better if I used a struct instead then? How would I do a struct that was accessible by every class?
my vertex class looks like this
class CVertices
{
D3DVECTOR3 Position;
D3DVECTOR3 Normal;
float Col1;
float Col2;
D3DVECTOR2 t1;
..t2;
..t3;
down to t8;
}
#define FVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_SPECULAR | D3DFVF_TEX8);
thanks for the help
de_matt
[edited by - de_matt on November 6, 2003 1:20:56 PM]
int *bob;
bob = new int[10];
delete bob;
I'm doing that and it works find for all my arrays except my vertex class array. When I try it with that I get an assertion message with "abort, retry, ignore". What could be causing this?
On the vertices class bit, would it be better if I used a struct instead then? How would I do a struct that was accessible by every class?
my vertex class looks like this
class CVertices
{
D3DVECTOR3 Position;
D3DVECTOR3 Normal;
float Col1;
float Col2;
D3DVECTOR2 t1;
..t2;
..t3;
down to t8;
}
#define FVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_SPECULAR | D3DFVF_TEX8);
thanks for the help
de_matt
[edited by - de_matt on November 6, 2003 1:20:56 PM]
As the previous poster said:
int *array;
array=new int[10];
delete [] array;
you MUST use [] when deleting array, in contrast...
int *variable;
variable=new int;
delete variable;
is the correct way to do it. If you are having problems with memory leaks, then you should be using the RAII methedology. This could be implemented in the case of memory, by using boosts encapsulated pointers. Equally, you should use similar methodology for all your resources (files etc).
int *array;
array=new int[10];
delete [] array;
you MUST use [] when deleting array, in contrast...
int *variable;
variable=new int;
delete variable;
is the correct way to do it. If you are having problems with memory leaks, then you should be using the RAII methedology. This could be implemented in the case of memory, by using boosts encapsulated pointers. Equally, you should use similar methodology for all your resources (files etc).
There''s also the auto_ptr, but of course it doesn''t do arrays if memory serves (does that in favor of using STL containers.)
Also, you may wish to get rid of the #define and use a happy const variable instead, but it may not like it because it''s using other #defines for the value. The general logic between structs and classes has (somewhat traditionally, or at least for me) is a struct would be better suited if there aren''t any methods. Of course that means nothing in C++, but be aware that a C++ structure starts in public scope mode, while a class starts out in private (or protected, can''t remember, but it''s not public)
Also, you may wish to get rid of the #define and use a happy const variable instead, but it may not like it because it''s using other #defines for the value. The general logic between structs and classes has (somewhat traditionally, or at least for me) is a struct would be better suited if there aren''t any methods. Of course that means nothing in C++, but be aware that a C++ structure starts in public scope mode, while a class starts out in private (or protected, can''t remember, but it''s not public)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement