SFML community forums

Help => Window => Topic started by: Oz1571 on December 03, 2009, 07:43:17 pm

Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: Oz1571 on December 03, 2009, 07:43:17 pm
I've tried searching the forums to see if someone else has been having this problem, but I didn't find anything.

My problem is that after updating my game from 1.5 to 2.0 it keeps crashing every time I exit the application. So I put together a minimal app to see if it still happens, and it does. I don't know whether it's an SFML issue, or a driver issue, or me screwing something up. I'm running the latest drivers for my graphics card.

So, the exact problem is that when I exit out of the app, it gives me an unhandled exception, and the issue appears to be on line 91 of ContextWGL.cpp:

Code: [Select]
   if (myContext)
    {
        if (wglGetCurrentContext() == myContext)
---->       wglMakeCurrent(NULL, NULL);
        wglDeleteContext(myContext);
    }


My call stacks looks like this:

    
Code: [Select]
   atioglxx.dll!6903a3ad()
  [Frames below may be incorrect and/or missing, no symbols loaded for atioglxx.dll]
  atioglxx.dll!690925d5()
  opengl32.dll!676e9286()
  opengl32.dll!676e9583()
> sfml-window-d.dll!sf::priv::ContextWGL::~ContextWGL()  Line 91 + 0xc bytes C++
  sfml-window-d.dll!`anonymous namespace'::`dynamic atexit destructor for 'referenceContext''()  + 0x28 bytes C++
  sfml-window-d.dll!_CRT_INIT(void * hDllHandle=0x00100000, unsigned long dwReason=0x00000000, void * lpreserved=0x00000001)  Line 449 C
  sfml-window-d.dll!__DllMainCRTStartup(void * hDllHandle=0x00100000, unsigned long dwReason=0x00000000, void * lpreserved=0x00000001)  Line 560 + 0x11 bytes C
  sfml-window-d.dll!_DllMainCRTStartup(void * hDllHandle=0x00100000, unsigned long dwReason=0x00000000, void * lpreserved=0x00000001)  Line 510 + 0x11 bytes C


And the code for my minimal program looks like this:

Code: [Select]
#include <SFML/Window.hpp>

int main()
{
sf::VideoMode mode;
mode.Width = 1280;
mode.Height = 800;
mode.BitsPerPixel = 32;

sf::Window Screen;
Screen.Create(mode, "oglgame");

return 0;
}


I'm going to feel really stupid if I'm just missing a line or two. :)

Edit: If I run in release instead of debug it instead seems to crash in crtdll.c on line 449:
Code: [Select]
                       /* call the function, which can eventually change __onexitbegin and __onexitend */
                        (*function_to_call)();

         ---->          onexitbegin_new = (_PVFV *)_decode_pointer(__onexitbegin);
                        onexitend_new = (_PVFV *)_decode_pointer(__onexitend);


And the call stack then looks like this:
Code: [Select]
atioglxx.dll!6903a3ad()
  [Frames below may be incorrect and/or missing, no symbols loaded for atioglxx.dll]
  atioglxx.dll!690925d5()
  opengl32.dll!67ed9286()
  opengl32.dll!67ed9583()
  sfml-window.dll!sf::priv::ContextWGL::~ContextWGL()  + 0x24 bytes C++
> sfml-window.dll!_CRT_INIT(void * hDllHandle=0x001c409c, unsigned long dwReason=0x001c4080, void * lpreserved=0x001c4080)  Line 449 C
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: Laurent on December 03, 2009, 08:46:18 pm
It looks like the same bug as in this topic:
http://www.sfml-dev.org/forum/viewtopic.php?t=1824
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: Oz1571 on December 04, 2009, 06:44:30 am
Ouch, and there's no solution to be found in that topic. :)
Should I post there or here? I guess I'll continue posting here since that person appears to have disappeared. Well, I guess I can tell you what I'm using:

Visual Studio 2008
ATI Radeon 5850, using the latest (9.11) drivers
Windows 7 64-bit

I'm going to see if I can try it out on another computer. I have a feeling this might be an ATI issue.

Edit: I tried it out on another computer running 64-bit Vista, 9.10 drivers and a 3000-series ATI card. Same crash. I'll see if I can bother someone with an nvidia/intel card tomorrow, but it looks like this might be an ati-only issue.
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: Laurent on December 04, 2009, 07:31:48 am
Don't bother trying on different computers, I'm sure that it is an ATI-only issue ;)
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: Oz1571 on December 04, 2009, 07:49:19 am
Yup, I just managed to convince a friend of mine with an Intel card to give it a go. It works just fine. Is there anything else you need from me that might help you track this down?
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: Laurent on December 04, 2009, 08:08:43 am
Send me an ATI card :lol:

Ok, more seriously, we can start testing these pieces of code:
Code: [Select]
#include <SFML/Window.hpp>

int main()
{
    sf::Window window(sf::VideoMode(800, 600), "crash!");
    return 0;
}

Code: [Select]
#include <SFML/Window.hpp>

int main()
{
    sf::Context context;
    return 0;
}

Code: [Select]
#include <SFML/Window.hpp>

int main()
{
    return 0;
}

1 and 2 should crash, I'm not sure about the last one.
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: Oz1571 on December 04, 2009, 08:19:29 am
They all crash, on the same line as my original code, except your last test, which works fine in Release but has the same crash in Debug. :)
If I had a spare ATI card I'd send you it!
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: Laurent on December 04, 2009, 09:07:50 am
Great.

Now create an empty project with no link to SFML, and try this code:
Code: [Select]
#include <windows.h>
#include <GL/gl.h>
#include <cassert>

class Context
{
public :

    Context(Context* shared = NULL) :
        myWindow       (NULL),
        myDeviceContext(NULL),
        myContext      (NULL)
    {
        myWindow = CreateWindowA("STATIC", "", WS_POPUP | WS_DISABLED, 0, 0, 1, 1, NULL, NULL, GetModuleHandle(NULL), NULL);
        assert(myWindow != NULL);
        ShowWindow(myWindow, SW_HIDE);

        myDeviceContext = GetDC(myWindow);
        assert(myDeviceContext != NULL);

        CreateContext(shared);
        assert(myContext != NULL);

        MakeCurrent();
    }

    ~Context()
    {
        if (wglGetCurrentContext() == myContext)
            wglMakeCurrent(NULL, NULL);
        wglDeleteContext(myContext);

        ReleaseDC(myWindow, myDeviceContext);
        DestroyWindow(myWindow);
    }

private :

    void MakeCurrent()
    {
        if (wglGetCurrentContext() != myContext)
            wglMakeCurrent(myDeviceContext, myContext);
    }

    void CreateContext(Context* shared)
    {
        PIXELFORMATDESCRIPTOR descriptor;
        ZeroMemory(&descriptor, sizeof(descriptor));
        descriptor.nSize        = sizeof(descriptor);
        descriptor.nVersion     = 1;
        descriptor.iLayerType   = PFD_MAIN_PLANE;
        descriptor.dwFlags      = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
        descriptor.iPixelType   = PFD_TYPE_RGBA;
        descriptor.cColorBits   = 32;
        descriptor.cDepthBits   = 24;
        descriptor.cStencilBits = 8;
        int format = ChoosePixelFormat(myDeviceContext, &descriptor);
        assert(format != 0);

        PIXELFORMATDESCRIPTOR desc;
        desc.nSize    = sizeof(desc);
        desc.nVersion = 1;
        int ok = SetPixelFormat(myDeviceContext, format, &desc);
        assert(ok != 0);

        myContext = wglCreateContext(myDeviceContext);
        assert(myContext != NULL);

        HGLRC sharedContext = shared ? shared->myContext : NULL;
        if (sharedContext)
        {
            ok = wglShareLists(sharedContext, myContext);
            assert(ok != 0);
        }
    }

    HWND  myWindow;
    HDC   myDeviceContext;
    HGLRC myContext;
};

Context c1;
Context c2(&c1);

int main()
{
    Context c3(&c1);
    return 0;
}
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: Oz1571 on December 04, 2009, 04:54:33 pm
Sorry about the late reply, I only just got back home.
The code you posted builds and runs without issues both in Debug and Release. Thanks for looking into this. :)
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: Laurent on December 04, 2009, 06:39:52 pm
Arg :(
It is the same code as in SFML, so I expected it to crash as well.

Does your application link to the dynamic version of SFML, or the static one?
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: Oz1571 on December 04, 2009, 07:17:18 pm
It links dynamically. Should I try linking it statically? I'll just have to figure out how to. :)

Edit: I just tried it with static linking, and it works just fine. No crashing! Even my full game works fine, not just the small tests.
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: Laurent on December 04, 2009, 10:52:25 pm
Ok, so that's always the same problem of globals and DLLs mixing badly :)

It won't be easy to solve... but at least there's an easy workaround.

Thanks a lot for your help ;)
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: panithadrum on December 05, 2009, 12:09:47 am
I hate dll's :D
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: Oz1571 on December 05, 2009, 12:22:46 am
You are very welcome! I'd be happy to try it out when/if you think you've found a fix, if I notice you post about it. :) Meanwhile, I'll stick to static dlls. Thanks again.
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: phear- on February 20, 2010, 03:15:01 am
Do you happen to be working on a fix Laurent? I figured its been over 2 months so I would ask. I would prefer not to use static for my games and I am having this bug.
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: Laurent on February 20, 2010, 10:17:46 am
No, sorry. It's hard to fix as it involves changing a lot of things in SFML.
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: GamerSg on June 21, 2010, 04:31:18 pm
Hi Laurent

  Firstly great work on SFML, its a breeze to use and gets the job done.

This problem however seems to affect all ATI users and affects a very large number of people. Im not sure whether the problem lies in SFML's code or ATI's drivers, however my guess is that it is likely SFML given the large number of OpenGL Applications which don't have this problem.

Im not familiar with the SFML source code, but if you want, i am willing to help you investigate further and root out the problem. Im sure there should be some way(hack if needed) to prevent this error.

If you are aware of the problem, maybe you could explain it here, so that we can try and come up with a solution.
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: Laurent on June 21, 2010, 05:10:34 pm
I'm not 100% sure, but I think that the ATI driver doesn't like my global OpenGL contexts.

I don't know who's wrong (I don't think that the OpenGL spec says something about this), but the only solution would be to create the main SFML context explicitely.

So if you want to help you can start by trying this solution, to make sure that it really solves the problem. The global contexts are declared in SFML/src/Window/GlContext.cpp. The second one can easily be removed and replaced with a sf::Context instance at the beginning of main(), but for the first one (the "reference context") you might have to hack a little bit :)
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: GamerSg on June 23, 2010, 08:36:47 am
Ill have a look when i have the time. Thanks.
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: arpeggiodragon on August 11, 2010, 06:09:49 am
Hi. Was a solution to this problem ever found? I'm using SFML 2.0 and experience this same crash with static linking on an Intel card. Is there a fix for this?

Thanks.

[EDIT] I missed reading the second page. So a fix might be in GLContext.cpp? I'll have a look even though I don't quite understand what needs to be changed.
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: Laurent on August 11, 2010, 08:42:49 am
There's no fix yet, sorry.
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: arpeggiodragon on August 11, 2010, 09:58:33 am
Ouch.
I'd be happy to help test this if you devise a fix for this issue. I also have an ATI Radeon card on my other computer, although I would have to check that it crashes first on that card.
Title: ContextWGL crash after updating from 1.5 to 2.0 beta
Post by: Laurent on August 11, 2010, 10:05:21 am
Ok, thanks :)