I have a program which uses the SDL library. Here is my function which updates the input state:
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:
int SDL_UpdateControls () {
...
return qevent;
}
void update () {
int qevent = 0;
qevent = SDL_UpdateControls();
if (qevent > 0)
rendering = 1;
}
This does not leak:
int SDL_UpdateControls () {
...
return 0;
}
void update () {
int qevent = 0;
qevent = SDL_UpdateControls();
if (qevent > 0)
rendering = 1;
}
This leaks:
void update () {
int qevent = 0;
qevent = SDL_UpdateControls();
if (qevent > 0)
rendering = 1;
}
This does not leak:
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!