Common programming error

Right after posting about a stupid but time-wasting bug, I have an actually tricky bug that I still should have caught.

I was doing a lazyfoo tutorial for SDL2, and I decided that I wanted to split up my work into a bunch of different functions, instead of having everything in main. Additionally, and this is what got me, I wanted to have as few global variables as possible. Instead of having my SDL_Window*, and my SDL_Surface* pointers I made them local in main.

Here’s the bug. I passed the pointers to a function Init(SDL_Surface* pSurface, SDL_Window* pWindow);
The function declaration should have been Init(SDL_Surface** ppSurface, SDL_Window** ppWindow);
That’s important because I ended using two SDL functions that return
1) a pointer to an SDL_Surface
2) a pointer to an SDL_Window
What that means, is that I was overwriting the pointer, instead of the memory that resided in the pointer. As a result, all the error checking worked, weirdly enough, even checking the main() pointers against NULL didn’t throw any errors which shouldn’t have happened.

Once I found the problem, it was a pretty easy fix. I didn’t initially realize that I had made the mistake twice, but once you figure one out, the other falls pretty fast.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s