Skip to main content
GameDev.net gamedev.net
🔒 Locked

Memory problem...

Started by Tree Penguin May 18, 2004 at 7:47 AM 12 replies 950+ views
Original Post
Tree Penguin
Tree Penguin
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!
jbb
jbb
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?
jbb
jbb
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?
twanvl
twanvl
Also, if you allocate an array using new you must use delete to free it when you are done with it.
Tree Penguin
Tree Penguin
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]
Goldfish
Goldfish
Are you sure the comparison with NULL is done? Try typing
OBJ* objects = new OBJ[1024];
instead, and see if that makes any difference.
2 + 2 = 5 for extremely large values of 2
Tree Penguin
Tree Penguin
Ok, i got what gave the error, i still don't understand why:

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]
jbb
jbb
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)
Tree Penguin
Tree Penguin
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]
jbb
jbb
Still sounds like a memory overwrite to me.
It might have happened any time previously in the program.
Tree Penguin
Tree Penguin
Ah, i got it, the 3 rotating values where just some trash values (like 46834590.221098530019992038954), making the string very long (over 255 characters) which indeed caused a memory overwrite of the text array in the glPrint function. Thanks for your help!

I guess when you allocate static variables / arrays they are set to a default value (0 or something), at least when debugging.

[edited by - Tree Penguin on May 18, 2004 9:40:30 AM]
Ysaneya
Ysaneya
Don't forget when you call new that the returned pointer will point to an uninitialized area of memory. I'd set the memory to 0 if i were you:

if (objects==NULL)
{
objects=new OBJ[1024];
memset(objects, 0, sizeof(OBJ) * 1024);
};



[edited by - Ysaneya on May 18, 2004 9:43:51 AM]
jbb
jbb
Don''t use memset to clear the memory allocated by new.
Make sure OBJ has a proper constructor to initialise the fields. That''s what it''s for.
Tree Penguin
Tree Penguin
I don't use a constructor for this class (i know i should), i initialize all variables when i load a model (so when an element gets used), i just didn't initialize those variables.

Ow, and when memset clears everything to 0, isn't 0 for a double the most negative value possible?

[edited by - Tree Penguin on May 18, 2004 9:53:25 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.