I’ve written 5 different versions of this question and I still don’t know what to ask.
I’m making a game in C with SDL. I’m on a Mac using Xcode. I made a basic main loop, and structures to hold tiles, tile maps, and levels, as well as a function to create a level from a json file (using cJSON). The state of my app is rather simple: it will just read a level from JSON, a texture file and try to render the level. I am doing all the stuff like checking for errors and null pointers, set pointers to null after freeing them etc… But every time I run my app WITHOUT MAKING ANY CHANGES TO THE CODE I get a different error on a different part of my program. Sometimes it actually runs just fine, then I hit run again and now there’s an error.
It’s really hard to ask because I really get very different errors for example:
I get an error on SDL_PollEvent
int shouldClose = 0;
while (!shouldClose) {
SDL_Event event;
while (SDL_PollEvent(&event)) { // Thread 1: signal SIGABRT
if (event.type == SDL_QUIT) {
shouldClose = 1;
break;
}
}
SQLevel_Render(level);
}
Upon closer inspection the error is in SDL_cocoaevents.m
in the function Cocoa_PumpEvents
in the following line:
NSEvent *event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
You would say the problem is clearly in my main loop, but then I run my program again and it works just fine, run it one more time and now the error is in
SQLevel_Render(level); // Thread 1: EXC_BAD_ACCESS (code=1, address=0xe2b5e925)
again with closer inspection the actual error is in SDL_Render.c
in the function SDL_RenderCopyF
in the line:
CHECK_TEXTURE_MAGIC(texture, -1);
these are just some of the errors that occur randomly when I run my app. There’s no specific order in which they occur and they don’t always occur (sometimes the app works just fine). Besides checking for SDL_QUIT
there’s no input, the program is just supposed to render the an image and do nothing else.
All of my code can be found in https://github.com/lsauceda/sour-quest in you want to take a look at it. Any help will be greatly appreciated.
UPDATE: Someone in Reddit pointed out that since my errors are so inconsistent I’m probably accessing some free
d memory or running out or an array’s bounds (that’s why sometimes it works and sometimes it doesn’t depending on what the invalid pointed data is). But in any way if someone wants to help me review my code I’ll really appreciate it (its not too large).