Does wglMakeCurrent() leak memory in a worker thread?

Hi, all:
I've been programming server-side offscreen rendering with OpenGL under MS Windows. The workflow consists of the following steps:
1. A client sends a request for volume rendering.
2. Server receives the request, forks a worker thread to do some ray-casting with parameters contained in the request.
3. Server retrieves the rendered image, sends it to the client and terminates the worker thread.

It works fine at first. However, after 10,000 requests, wglMakeCurrent() will fail and significant memory leak can be detected. So I make a simplified test program which contains only OpenGL context creation and deletion. It's shown below. wglMakeCurrent() always fails after about 10,000 loops. Can somebody tell me if there's anything wrong with the code? I'm using Nvidia Quadro GPU, the driver version is 307.45, OS is Windows 7 64-bit.

I found something confusing with this test program. wglMakeCurrent() creates a Windows user object every time it's called, but this object is not released in wglDeleteContext(). It still exists even after the worker is terminated, causing a memory leak. There is a per-process limit of user objects under Windows, so the program will eventually fail.

When the code of context creation/deletion is moved to the main thread, wglMakeCurrent() doesn't create new user object after the first invocation. So it seems that wglMakeCurrent() only create new user object in new threads. But since the OpenGL context is explicitly deleted and the thread is terminated, resources associated with that context should be released as well. I'm not sure it's the fault with my code or the driver. Can somebody help me with this? Thanks.

1. You don't need to register new window classes in every thread (unless they are different).
2. Your graphic card won't change during runtime, so you don't need to choose a pixel format for every DC. Just choose for the first one and save the value for later.
3. The problem is in the OpenGL implementation for sure. Each thread initializes one object when ogl is used(!), and, so it seems, not released when the thread is destroyed. Try using a thread pool. This way you lose the overhead for thread creation. And maybe you can re-use the already initialized render contexts and windows.