Error: "Renderer already associated with window"

Hello all,

I have a SDL2 project that I built so far successfully in an Ubuntu 15.04 VM. Now I assembled a new Desktop and just freshly installed Ubuntu 15.04 there as well as Code::Blocks and SDL2. I cloned my git repository, opened the Code::Blocks project and get the following error from SDL_GetError():

“Renderer already associated with window”

The call resulting in this error is

Code:
SDL_Renderer *ren = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_SOFTWARE);

I already tried substituting SDL_RENDERER_SOFTWARE by SDL_RENDERER_ACCELERATED, but the error is the same. Am I missing something obvious here? Could someone please give me a nudge in the right direction?

Thanks!

It is important for you to share the whole code if you want us to help you, and not only one line of it.

I’m sorry, I thought that the error message might be enough already. Here’s the full code up to the point where the error happens (all code after includes about 12 own classes and is not called up to that point):

Code:

SDL_Window* gWindow = NULL;
SDL_Surface* gScreenSurface = NULL;
SDL_Surface* renderSurface = NULL;

bool init()
{
//Initialization flag
bool success = true;

//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
	printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
	success = false;
}
else
{
	//Create window
	gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, defaultConfiguration.getScreenWidth(), defaultConfiguration.getScreenHeight(), SDL_WINDOW_SHOWN );
	if( gWindow == NULL )
	{
		printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
		success = false;
	}
	else
	{
            //Initialize PNG loading
                int imgFlags = IMG_INIT_PNG;
                if( !( IMG_Init( imgFlags ) & imgFlags ) )
                    {
                printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
                success = false;
                    }
                else
                    {
                        //Get window surface
                        gScreenSurface = SDL_GetWindowSurface( gWindow );
                        renderSurface = SDL_CreateRGBSurface(0,640,480,32,0,0,0,0);
                    }
            }
}
 //Initialize SDL_ttf
 if( TTF_Init() == -1 )
    {
        printf( "SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError() );
        success = false;
    }
return success;

}

int main( int argc, char* args[] )
{
//Start up SDL and create window
if( !init() )
{
printf( “Failed to initialize!\n” );
}
else
{
SDL_Renderer *ren = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_SOFTWARE);
if (ren == nullptr){
SDL_DestroyWindow(gWindow);
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
//Here comes a lot of SDL unrelated code that’s never executed because the ren is null and the error gets caught

Sorry, I missed the includes (some of them only used after the point of failure):

Code:

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include
#include
#include
#include <unordered_map>

My guess is that SDL_GetWindowSurface() has to create a hidden renderer so
you can access the window like a surface. If you’re using the renderer
API, don’t use SDL_GetWindowSurface().

See:
https://wiki.libsdl.org/SDL_GetWindowSurface

Jonny DOn Tuesday, July 28, 2015, Murenius wrote:

Sorry, I missed the includes (some of them only used after the point of
failure):

Code:

#include
#include
#include
#include
#include
#include
#include
#include

Thanks for the hint, I’ll check this. But if it is the case I wonder why it works in the VM on another machine.

@Jonny D is correct.

SDL_GetWindowSurface calls SDL_CreateWindowFramebuffer which calls internal CreateWindowFramebuffer (SDL_CreateWindowTexture) in which SDL_Renderer is created.

Thanks a huge lot! :slight_smile: That indeed did the trick. I wonder, though, how this could run at all in the VM. [Shocked]