Memory problem...
Hi, i got a problem:
When i do this:
OBJ objects[1024];
everything goes fine, but when i do this:
OBJ *objects=NULL;
and then in a function that loads an object i do this the first time a model is loaded:
if(objects==NULL){objects=new OBJ[1024];};
The object is correctly loaded after this but for some reason when i draw the object data using the glPrint function there is access violation at the line:
glListBase(base);
So, is there any difference between allocating the array at startup and allocating it using new?
Thanks in advance!
It should be fine.
You''re not passing "objects" as a parameter to your model loading function are you and altering just the local copy inside the function?
You''re not passing "objects" as a parameter to your model loading function are you and altering just the local copy inside the function?
It should be fine.
You''re not passing "objects" as a parameter to your model loading function are you and altering just the local copy inside the function?
You''re not passing "objects" as a parameter to your model loading function are you and altering just the local copy inside the function?
Also, if you allocate an array using new you must use delete to free it when you are done with it.
I allocate the array the first time a model is loaded, it is free-ed at the end of the program as the data inside is needed in the whole program as it contains the models that have to be drawn. So is new[] scope-limited or something?? I guess not, also because malloc doesn't work either.
This is what i do now:
OBJ *objects=NULL;
char *OnLoadObject(unsigned int type){
if(objects==NULL){
objects=new OBJ[1024];
};
(...)
This does work fine:
OBJ objects[1024];
char *OnLoadObject(unsigned int type){
(...)
[edited by - Tree Penguin on May 18, 2004 9:10:56 AM]
This is what i do now:
OBJ *objects=NULL;
char *OnLoadObject(unsigned int type){
if(objects==NULL){
objects=new OBJ[1024];
};
(...)
This does work fine:
OBJ objects[1024];
char *OnLoadObject(unsigned int type){
(...)
[edited by - Tree Penguin on May 18, 2004 9:10:56 AM]
Are you sure the comparison with NULL is done? Try typing
OBJ* objects = new OBJ[1024];
instead, and see if that makes any difference.
OBJ* objects = new OBJ[1024];
instead, and see if that makes any difference.
2 + 2 = 5 for extremely large values of 2
Ok, i got what gave the error, i still don't understand why:
somewhere in the ui drawing code i had this:
For some reason this was invalid when using new, any ideas?
EDIT:
OBJ *objects=new OBJ[1024];
Doesn't help.
[edited by - Tree Penguin on May 18, 2004 9:26:21 AM]
somewhere in the ui drawing code i had this:
if(m_selected){ glPrint("Object %i, o= (%f,%f,%f), r=(%f,%f,%f)",m_nselected, m_objects[m_nselected]->o.x,m_objects[m_nselected]->o.y,m_objects[m_nselected]->o.z, m_objects[m_nselected]->xr,m_objects[m_nselected]->yr,m_objects[m_nselected]->zr); };
For some reason this was invalid when using new, any ideas?
EDIT:
OBJ *objects=new OBJ[1024];
Doesn't help.
[edited by - Tree Penguin on May 18, 2004 9:26:21 AM]
My feeling would be that you are accessing beyond the end of the 1024 elements somewhere in your program.
Because the global array and the malloced memory will be at different memory locations you''re overwritting something different in each case so one crashes and the other is probably breaking something else you''ve not noticed yet
I''d look for accessing beyond the end of the array (or before the start)
Because the global array and the malloced memory will be at different memory locations you''re overwritting something different in each case so one crashes and the other is probably breaking something else you''ve not noticed yet
I''d look for accessing beyond the end of the array (or before the start)
When i get the error, m_nselected is 0, just the first element, when i add another object and i select that one m_nselected is 1 (just like it should). When i enter the glPrint() function (a member of that class) all variables are invalid (ERROR: expression could not be evaluated) including the 'base' variable. Any thoughts?
[edited by - Tree Penguin on May 18, 2004 9:31:16 AM]
[edited by - Tree Penguin on May 18, 2004 9:31:16 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement