| | |
SDL library memory leak
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2007
Posts: 31
Reputation:
Solved Threads: 0
I have a program which uses the SDL library. Here is my function which updates the input state:
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:
This does not leak:
This leaks:
This does not leak:
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!
C Syntax (Toggle Plain Text)
int SDL_UpdateControls () { int qevent = 0; while(SDL_PollEvent(&event)) { qevent = 1; switch(event.type) { case SDL_KEYDOWN: SDL_keydown = event.key.keysym.sym; break; case SDL_KEYUP: SDL_keyup = event.key.keysym.sym; break; case SDL_MOUSEBUTTONDOWN: SDL_mousedown = 1; SDL_mousedx = event.button.x; SDL_mousedy = event.button.y; break; case SDL_MOUSEBUTTONUP: SDL_mouseup = 1; SDL_mouseux = event.button.x; SDL_mouseuy = event.button.y; break; case SDL_MOUSEMOTION: SDL_mousemove = 1; SDL_mousex = event.motion.x; SDL_mousey = event.motion.y; break; case SDL_QUIT: if (SDL_onquit != NULL) { void(*eventfunction)() = SDL_onquit; eventfunction(); } break; } } int c; for (c = 0;c < SDL_TotalControls;c++) { SDL_UpdateControl(c); } return qevent; }
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:
C Syntax (Toggle Plain Text)
int SDL_UpdateControls () { ... return qevent; } void update () { int qevent = 0; qevent = SDL_UpdateControls(); if (qevent > 0) rendering = 1; }
This does not leak:
C Syntax (Toggle Plain Text)
int SDL_UpdateControls () { ... return 0; } void update () { int qevent = 0; qevent = SDL_UpdateControls(); if (qevent > 0) rendering = 1; }
This leaks:
C Syntax (Toggle Plain Text)
void update () { int qevent = 0; qevent = SDL_UpdateControls(); if (qevent > 0) rendering = 1; }
This does not leak:
C Syntax (Toggle Plain Text)
void update () { int qevent = 0; SDL_UpdateControls(); // not using return variable if (qevent > 0) rendering = 1; }
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!
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 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.
•
•
Join Date: Sep 2007
Posts: 31
Reputation:
Solved Threads: 0
•
•
•
•
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".
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!
![]() |
Other Threads in the C Forum
- Previous Thread: Need Help With Homework
- Next Thread: Reading Files with fgetc?
| Thread Tools | Search this Thread |
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string strings structures suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






