Windows: Function loading issue.

Issue #4 resolved
Jason McKesson
created an issue

The Win32 version of GetProcAddr looks like this:



void* p = wglGetProcAddress(function_name); if (p) return p; HMODULE h = LoadLibraryA("opengl32.dll"); return GetProcAddress(h, function_name); }}}

This is a clever way to get around the fact that wglGetProcAddress will not return an address for core OpenGL functions. The problem is this: Windows keeps an internal reference count for loaded DLLs. Every time you call LoadLibraryA, you will bump this reference count. Considering how many functions are in GL 1.1, that could be quite a lot.

Unless you properly release this reference count with FreeLibraryA, you will basically make it very difficult for any user who tries to load the OpenGL32.dll library dynamically rather than statically.

Comments (3)

  1. yotis repo owner

    I will work on this issue the next days. A static HMODULE onto which, if not null, the library's various destructors will try to apply FreeLibrary().

  2. Jason McKesson reporter

    Note that you can probably use GetModuleHandle to get the module handle for OpenGL32.dll. That doesn't bump the reference count, so you don't have to call FreeLibrary after.

  3. yotis repo owner

    Commit 20f7d0f4ca0a fixes this issue. Note that this commit also brakes the API for the versions (see issue #5 comments), so I am attaching the following snippet for anyone who wishes to use this fix on the 0.3 tag, until the API is stable again with the upcoming 0.4 version tag.

    Replace GetProcAddress(){...} for the windows section in glxx.cpp (line 90) with the following

        struct opengl32_dll_wrapper {
            HMODULE h;
                h = LoadLibraryA("opengl32.dll");
        std::auto_ptr<opengl32_dll_wrapper> opengl32_dll;
        void* GetProcAddr(const char* function_name)
            void* p = wglGetProcAddress(function_name);
            if (p)
                return p;
            if (opengl32_dll.get() == 0)
                opengl32_dll.reset(new opengl32_dll_wrapper);
            return GetProcAddress(opengl32_dll->h, function_name);

    I saw the comment about GetModuleHandle too late, i will consider this solution in the future.

  4. Log in to comment