Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SOLVED: CRASH: Sharing GL context in multiple windows  (Read 2113 times)

0 Members and 1 Guest are viewing this topic.

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
SOLVED: CRASH: Sharing GL context in multiple windows
« on: January 20, 2013, 02:08:25 am »
Hi all:

I'm using SFML 2.0 rc 118 on Win7 with VS2012 express.

I'm attempting to toggle fullscreen by creating a new sf::RenderWindow.  After the new window is created, I destroy the previous one.  This is working fine up until I shut down, when textures are free'd.  I'm getting an "Access violation" error, similar to the kind of error you see when you attempt to destroy a texture after the GL context has been destroyed.

Here's a small program to reproduce the issue:

#include <memory>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>


///////////////////////////////////
typedef std::unique_ptr<sf::RenderWindow>       WindowPtr;
WindowPtr                   wnd;

void makeItCrash()
{
    // toggle from windowed to fullscreen
    WindowPtr next( new sf::RenderWindow( sf::VideoMode::getDesktopMode(), "Test", sf::Style::Fullscreen ) );

    // reassign the 'next' window, so the previous windowed window is destroyed
    //   and is replaced with the new fullscreen window
    wnd = std::move(next);
}

int main()
{
    // Create a windowed window
    wnd = WindowPtr( new sf::RenderWindow( sf::VideoMode(400,400), "Test" ) );

    {
        // all resources in local scope, to ensure they are destroyed before the final window is destroyed

        // load up a texture.
        std::unique_ptr<sf::Texture> atexture( new sf::Texture );
        atexture->loadFromFile("image.png");

        // if I comment out this call, it works fine
        //   this call basically replaces 'wnd' so that it points to a fullscreen window
        //   instead of a windowed window.
        makeItCrash();

    }       // texture destroyed here.      !!! This is where the crash actually happens !!!
      // !!! when the texture is destroyed !!!!


    // manually destroying the window here
    wnd.reset();

    return 0;
}
 

The best guess I can make of the problem is that the GL context is being destroyed when I destroy the original window.  Then, when I try to destroy the texture, it craps out because the context it was created in no longer exists.

This is baffling though because drawing works just fine in the new window.  So the context must still exist, right?


What am I doing wrong here?  I know SFML is capable of sharing a context between multiple windows, so it shouldn't matter when I destroy the first window, as long as any window is still in play.

Any help appreciated.  Thanks.
« Last Edit: January 20, 2013, 04:26:13 am by Disch »

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Re: CRASH: Sharing GL context in multiple windows
« Reply #1 on: January 20, 2013, 04:25:55 am »
SOLVED:

Looks like this was a problem with my video drivers.  I updated for an unrelated reason, but the update seems to have fixed this issue as well.

Nice.