SDL library memory leak

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2007
Posts: 31
Reputation: csteverun is an unknown quantity at this point 
Solved Threads: 0
csteverun csteverun is offline Offline
Light Poster

SDL library memory leak

 
0
  #1
Feb 18th, 2008
I have a program which uses the SDL library. Here is my function which updates the input state:

  1. int SDL_UpdateControls () {
  2. int qevent = 0;
  3. while(SDL_PollEvent(&event)) {
  4. qevent = 1;
  5. switch(event.type) {
  6. case SDL_KEYDOWN:
  7. SDL_keydown = event.key.keysym.sym;
  8. break;
  9. case SDL_KEYUP:
  10. SDL_keyup = event.key.keysym.sym;
  11. break;
  12. case SDL_MOUSEBUTTONDOWN:
  13. SDL_mousedown = 1;
  14. SDL_mousedx = event.button.x;
  15. SDL_mousedy = event.button.y;
  16. break;
  17. case SDL_MOUSEBUTTONUP:
  18. SDL_mouseup = 1;
  19. SDL_mouseux = event.button.x;
  20. SDL_mouseuy = event.button.y;
  21. break;
  22. case SDL_MOUSEMOTION:
  23. SDL_mousemove = 1;
  24. SDL_mousex = event.motion.x;
  25. SDL_mousey = event.motion.y;
  26. break;
  27. case SDL_QUIT:
  28. if (SDL_onquit != NULL) {
  29. void(*eventfunction)() = SDL_onquit;
  30. eventfunction();
  31. }
  32. break;
  33. }
  34. }
  35. int c;
  36. for (c = 0;c < SDL_TotalControls;c++) {
  37. SDL_UpdateControl(c);
  38. }
  39. return qevent;
  40. }

The qevent variable was best way I could think of to capture whether or not an event happened. My program needs to know this, so it knows whether or not to re-render the graphics. The problem is that whenever I use this value, I get some kind of memory leak.

It's very strange. Every time I move the mouse the program eats up about 10MB of memory. You can imagine how quickly this causes an out of memory error. The strange part is, it works fine (no memory leak) as long as I never use the qevent variable.

For example, this causes a leak:

  1. int SDL_UpdateControls () {
  2.  
  3. ...
  4.  
  5. return qevent;
  6. }
  7.  
  8. void update () {
  9. int qevent = 0;
  10. qevent = SDL_UpdateControls();
  11. if (qevent > 0)
  12. rendering = 1;
  13. }

This does not leak:

  1. int SDL_UpdateControls () {
  2.  
  3. ...
  4.  
  5. return 0;
  6. }
  7.  
  8. void update () {
  9. int qevent = 0;
  10. qevent = SDL_UpdateControls();
  11. if (qevent > 0)
  12. rendering = 1;
  13. }

This leaks:

  1. void update () {
  2. int qevent = 0;
  3. qevent = SDL_UpdateControls();
  4. if (qevent > 0)
  5. rendering = 1;
  6. }

This does not leak:

  1. void update () {
  2. int qevent = 0;
  3. SDL_UpdateControls(); // not using return variable
  4. if (qevent > 0)
  5. rendering = 1;
  6. }

I don't see any logic to it. It must be some strange C memory thing, but I can't figure it out. Any ideas?

Thx!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,038
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: SDL library memory leak

 
1
  #2
Feb 18th, 2008
Very eloquent title for your post.
You are looking at the wrong variable. You are paying attention to qevent when you should pay attention at rendering = 1;
From here what I see is that when the if (qevent > 0) gets executed, it sets rendering = 1;
and according to you [it] "leaks".
Last edited by Aia; Feb 18th, 2008 at 9:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 31
Reputation: csteverun is an unknown quantity at this point 
Solved Threads: 0
csteverun csteverun is offline Offline
Light Poster

Re: SDL library memory leak

 
0
  #3
Feb 18th, 2008
Originally Posted by Aia View Post
Very eloquent title for your post.
You are looking at the wrong variable. You are paying attention to qevent when you should pay attention at rendering = 1;
From here what I see is that when the if (qevent > 0) gets executed, it sets rendering = 1;
and according to you [it] "leaks".
You were right. The rendering = 1 part did lead to the problem. I thought I had already tested that, so I never tried that again. It turned out to be a perfectly reasonable problem. I had assumed SDL was reusing surfaces, but it was not. A few calls to SDL_FreeSurface() fixed it.

Thx again!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC