Archived

This topic is now archived and is closed to further replies.

Memory problem...

This topic is 4961 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites