|
Another newbie question (100% cpu usage and memory leak) |
Justin Slowik
Member #7,235
May 2006
|
hey i'm working on getting the basic components down for a game I am working on in my spare time, and I have just started doing keyboard initiated movement. However,if I open the task manager while the program is running, it shows CPU usage at 100%, and if I move my character up and down, my pagefile usage and overall memory usage slowly climb. Here's the main parts of my movement code: Main (skipped over basic initialization code): while(!(key[KEY_ESC])){ //begin main game loop if (keypressed()) { animatePC(character_data,landFile,playerPos); clear_keybuf(); } } animatePC:
Doublebuffer Code: void DrawDoublebuffer(BITMAP *buffer) { acquire_screen(); blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); release_screen(); } and finally Drawmap
any help for fixing either of these problems would be greatly appreciated. as far as I can tell I'm destroying all bitmaps after they're used, so I dont know why the memory usage continues to climb every time I move in a direction. As for the 100% cpu, I'm assuming its cause I'm constantly checking for a keypress. if thats the reason for it, is there an easy way to fix this? any help would be greatly appreciated. thanks |
Matthew Leverton
Supreme Loser
January 1999
|
Regarding 100% CPU usage, you'll have to throw a rest(1) in there somewhere. Keep in mind that it will probably rest for 10ms. Or you can rest(0) which "plays nice," but still uses 100% CPU. |
gnolam
Member #2,030
March 2002
|
And as for the memory leak, you never destroy "buff" in draw_map(). That said, don't recreate your buffers every time. It's horribly inefficient. Create them once at the start of the program and destroy them once at the end. Also, look into separating your logic and your drawing... [EDIT] -- |
Justin Slowik
Member #7,235
May 2006
|
thanks for the rest() idea. that fixed the cpu time a bunch! now as for the leak I seem to be having, i realized its not my main memory thats leaking anymore, it does increase while moving but when I stop it goes back down again. however, my pagefile usage does climb slowly but surely. And I'm afraid after an extended period of time I'll overflow. Any other ideas on that? the pagefile seems to climb 1-5 MB every time I move 1 tile space. [edit] |
ReyBrujo
Moderator
January 2001
|
I believe your code has a great memory leak. See function animatePC. You create a buffer. But you only destroy it a couple of times inside for cycles. Your function should be like this instead:
-- |
Justin Slowik
Member #7,235
May 2006
|
ReyBrujo, I tried your idea, and it just made my pagefile leakage climb at least 2 or 3 times faster. |
ReyBrujo
Moderator
January 2001
|
Hehehe, the problem is that draw_map is creating bitmaps that are never being destroyed too. This code should work. Don't create a bitmap inside animatePC:
-- |
Justin Slowik
Member #7,235
May 2006
|
Thanks Rey! that fixed it now. I appreciate the help now i cna move on w/ my life hah. |
A J
Member #3,025
December 2002
|
create_bitmap() is a signifacnt cost, only do it if you have to. ___________________________ |
|