Jump to content
  • Advertisement

Archived

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

Tree Penguin

Memory problem...

This topic is 5173 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
Advertisement
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
Also, if you allocate an array using new you must use delete to free it when you are done with it.

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
Are you sure the comparison with NULL is done? Try typing
OBJ* objects = new OBJ[1024];
instead, and see if that makes any difference.

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
Still sounds like a memory overwrite to me.
It might have happened any time previously in the program.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!