Sign in to follow this  
midtown266

value trouble in my main loop

Recommended Posts

Hey guys and gals, I'm having a little bit of a problem. My program runs a little black box with random arrays of numbers scrolling down it. Kind of like those matrix screensavers, so I guess it might be called some matrix array scroll program, or somewhat. It works and all, unless I have the value for the number of arrays inbetween, and including 1000 - 1010. I am totally stumped. This is the main loop that does stuff. It fails on the sixth time around.
Quote:
glClearColor (0.0f, 0.0f, 0.0f, 0.0f); glClear (GL_COLOR_BUFFER_BIT); glColor3f(0.0f, 1.0f, 0.0f); for( int i=0; i<size; i++) { if( array[i].current_cycle == array[i].cycles_on_screen ) //checks to see array[i] has lived its life. { delete &(array[i]); temp = new randlist; array[i] = *temp; } else array[i].draw_list(); // draws array[i] } SwapBuffers (hDC); Sleep (35);
The only thing that I can think of is something to do with the delete, deleting something that isnt there, because Ive had that problem before. I've found out that it stops working in the sixth cycle of the main loop, but I'm having a lot of trouble narrowing it down further than that. thanks for any help, -Eric- this is with a window the size of 256x256, 800x800, & 1600x1200. There is another problem that when I change the values of the scalars for the numbers, to make them skinnier/wider/taller/shorter, for different window sizes to get rid of distortion, that it just stops after a couple of cycles. I have not looked into this as thoroughly, but any suggestions here are welcome as well. I didn't want to include all of the code for the randlist struct and the showNumber() function, but if you want to see it, or any other code from this file, just ask. [Edited by - midtown266 on November 10, 2005 12:16:38 AM]

Share this post


Link to post
Share on other sites
Quote:

delete &(array[i]);
temp = new randlist;
array[i] = *temp;


This doesn't do what you think it does.

First, it passes an actual pointer into the array to delete -- that has likely never been returned from new.

Second, it copies the allocated object into the index in the array.

Third, it leaks the actually allocated "temp" object.

Share this post


Link to post
Share on other sites
My bad >_<

It has most definitely been a while since I have done this :) I had just written a pretty long post refuting your insightfulness, but I have realized the errors of my ways ^_^.

I made array a randlist ** instead, and the resulting code is

Quote:

for( int i=0; i<size; i++)
{
if( array[i]->current_cycle == array[i]->cycles_on_screen ) //checks to see if array[i] has lived its life.
{
delete array[i];
array[i] = new randlist;
}
else
array[i]->draw_list(); // draws array[i]
}


the pointers introduce a slight performance hit, nothing noticeable, but now I have no more memory leaks here!!! --hopefully--

Thanks mate.
-Eric-

WHOOOOOOOOO!!!! It appears to have fixed both of the problems I previously listed ^o^ I am currently adding in some more functionality, but I will post the problems in the near future if I don't resolve them, because now, I'm off to bed ya'll :D.

[Edited by - midtown266 on November 10, 2005 12:58:47 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this