(bug) Basic SDL code causing read violation (kernel32.dll)

Hi, I’m currently looking into converting my team’s development platform to SDL2.
Things started fine, but I’m getting a read access violation for the following code:

int main( int argc, char* argv[] ) {
SDL_Init( SDL_INIT_VIDEO );

auto gWindow = SDL_CreateWindow( u8"Nice", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN );
auto gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );

return 0;
}

Error (Visual Studio 2015, Windows 10):

Exception thrown at 0x749F49AA (kernel32.dll) : 0xC0000005: Access violation reading location 0x72D5EAF0.

I wasn’t sure if this would be something that I can just overlook, this seems like simple code which should be running just smoothly.

For reference, if I comment out the line with the renderer, things end fine. It’s only with the renderer that the exception throws.

Update

I notice that if I change the renderer settings to

SDL_RENDERER_SOFTWARE

instead, then things end fine. Would someone happen to know what might be happening?

An access violation means you are trying to write to an uninitialized variable. I have never heard of an auto data type, it is usually SDL_Renderer data type initialized as NULL. Here is some example code.

https://wiki.libsdl.org/SDL_CreateRenderer

the auto keyword was re-purposed by the 2011 c++ standard to mean “deduce the type here”. see http://en.cppreference.com/w/cpp/language/auto

SDL uses return values to determine when errors occur. For SDL_Init(), it’ll return a non-zero value. For SDL_CreateWindow() and SDL_CreateRenderer(), it’ll return NULL (aka. a ‘zero’-value pointer).

Are any of those functions indicating error, via their return values?

– David L.

Both SDL_CreateWindow() and SDL_CreateRenderer() return non-null values, hence they seem to fire properly.
I can also render images, etc. perfectly fine.
If I go through the debugger in visual studio, the error occurs once I return from int main().

The debugger then goes into static int main_utf(),
followed by static int main_getcmdline()
(into retval = main_utf8(argc, argv);)
the retval here also returns 0.

Then we pass into

int WINAPI
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
{
    return main_getcmdline();
}

which follows into:

static __declspec(noinline) int __cdecl __scrt_common_main_seh() throw()

and within this function, it throws an exception at

if (!__scrt_is_managed_app())
    exit(main_result);