C++ Winapi handles grows up to 1800 with pixelformatdescriptor

Hello guys, I have one serious problem with pixelformatdescriptor

static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};

if(!(hdc = GetDC(hwnd)))							// Did We Get A Device Context?
{
	KillGLWindow();								// Reset The Display
	ShowCursor(true);
	MessageBox(NULL, "Can't Create A GL Device Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
	return FALSE;								// Return FALSE
}
//DescribePixelFormat(hdc, PixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
// 650 handles
if(!(PixelFormat = ChoosePixelFormat(hdc, &pfd)))	// Did Windows Find A Matching Pixel Format?
{
	KillGLWindow();								// Reset The Display
	ShowCursor(true);
	MessageBox(NULL, "Can't Find A Suitable PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
	return FALSE;								// Return FALSE
} 
// + another 1100 handles
if(!SetPixelFormat(hdc, PixelFormat, &pfd))		// Are We Able To Set The Pixel Format?
{
	KillGLWindow();								// Reset The Display
	ShowCursor(true);
	MessageBox(NULL, "Can't Set The PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
	return FALSE;								// Return FALSE
} 
if(!(hRC = wglCreateContext(hdc)))				// Are We Able To Get A Rendering Context?
{
	KillGLWindow();								// Reset The Display
	ShowCursor(true);
	MessageBox(NULL, "Can't Create A GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
	return FALSE;								// Return FALSE
}
if(!wglMakeCurrent(hdc, hRC))					// Try To Activate The Rendering Context
{
	KillGLWindow();								// Reset The Display
	ShowCursor(true);
	MessageBox(NULL, "Can't Activate The GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
	return FALSE;								// Return FALSE
}
if(!InitGL())									// Initialize Our Newly Created GL Window
{
	KillGLWindow();								// Reset The Display
	ShowCursor(true);
	MessageBox(NULL, "Initialization Failed.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
	return FALSE;								// Return FALSE
}

Everything works like a charm but one thing. Handles of the application radicaly increase once I ChoosePixelFormat - handles in TaskMgr grow up to 650 handles and after wglCreateContext it increases of another 1100 handles, so my app ends up with something around 1800 handles. My question is simple, is that radical handle increase normal?
Thank you all in advance :wink:

What version of Windows® and what graphics hardware/driver are you using? In my experience, some graphics drivers are extremely resource wastefull when creating GL contexts.
I just tried two small sample programs on a virtualBox virtual XP machine and the task manager indicated some 43 handles.

Thank you for a quick reply.
I am runing on Windows 8 and on AMD Radeon HD7770 VaporX GPU with the latest 14.100.0.0 driver. My collegue has the same problem with Windows 8.1 on AMD Radeon as well.

I guess that’s typicall for AMD. The last time I tested glX and xrender programs on a CentOS system with the proprietary AMD driver I noticed that they consumed and leaked quite a lot of memory.

Do you actually have a real problem or do you just wonder there are so many handles?

I am just wondering, why the pixelformatdescriptor takes so many handles…it seems really weird to me…

So when I run the app on nVidia GPU, it won’t take so many handles?

I honestly think you’re just being paranoid counting handles like this. On a test app, using Windows 8.1, I get ~20,000 handles in use during normal operation with just Firefox, Visual Studio and a File Explorer window open.

There are plenty of reasons why Windows, a driver or an app might create additional handles and they’re not necessarily all related to being “wasteful” or leaking resources.

A HANDLE is just a generic data type used by many Windows API calls; MSDN defines the following object types that may use one (I’m using an older version of MSDN here so doubtless the list has since grown):

  • Accelerator tables.
  • Bitmaps.
  • Brushes.
  • Color spaces.
  • DDE stuff.
  • Device contexts.
  • The desktop.
  • Drag and drop.
  • Window positioning.
  • Metafiles.
  • Fonts.
  • GDI objects.
  • Global memory blocks.
  • Hooks.
  • Icons.
  • Program instances.
  • Registry keys.
  • Local memory blocks.
  • Etc etc etc.

If you look at Archived MSDN and TechNet Blogs | Microsoft Learn you’ll see that you can create ~16,000,000 handles on both 32-bit and 64-bit Windows before you exhaust resources, so unless you have actual evidence of a real leak (i.e the handle count constantly rising until something crashes) then you really don’t need to worry about it (I can virtually guarantee that you’re nowhere near that limit), and the problem is definitely not as serious as you think (I’d actually argue as to whether or not it’s even a “problem”).

Ok, thank you very much!
I was just wondering about it becouse most modern games don’t have even a third of what my app has. Not a serious problem, once again, thank you and have a nice day :slight_smile:

Most modern games use Direct3D, not OpenGL, so you’re not really comparing like-with-like.

Just for the record:

On NVidia I get a report of approx. 250 handles being used when starting an application that opens a GL context. It seems to be an issue with AMD’s driver. But as long as all these handles only get allocated once, don’t worry. What you should check is if they leak if you try to destroy and recreate the context, provided that you even do such a thing.

I still find OpenGL better than DirectX, but these little things lead me to switch to DirectX…I don’t really know, what is better for making games today…

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.