windows - System resources not being released -
i have problem system resources seemingly not being returned on windows 7. have written short test program in c nothing except create bunch of windows destroys them in infinite loop. after each iteration of loop handles count in task manager window goes , never returns starting value. also, memory used creeps up. after running program long enough crashes. need able create , destroy opengl contexts. if include opengl in simple test app behavior same except uses resources faster. missing something?
// resourcetest.cpp : defines entry point console application. // #include "stdafx.h" #include "windows.h" #include "../gl/gl.h" #include "../gl/glext.h" #include "../gl/wglew.h" typedef struct _windowinfo { int x, y; int width, height; glboolean visible; char *title; hwnd hwnd; hdc device_context; glint id; glint pixelformat; hglrc hglrc; } windowinfo; typedef glboolean (apientry *choosepixelformatarbfunc_t)(hdc, glint *, glfloat *, gluint, glint *, gluint *); choosepixelformatarbfunc_t choosepixelformatarb_func; #define window_name window->title handle windowdestroyed; long winapi mainwndproc( hwnd hwnd, uint umsg, wparam wparam, lparam lparam ) { /* int w,h; */ switch ( umsg ) { case wm_create: { hdc hdc; int attriblist[100]; float fattriblist[] = { 0.0, 0.0 }; int = 0; windowinfo *window = (windowinfo *)(((lpcreatestruct)lparam)->lpcreateparams); pixelformatdescriptor *ppfd = null; hdc = getdc(hwnd); if (window->pixelformat <= 0) { pixelformatdescriptor pfd = { sizeof(pixelformatdescriptor), /* size of pfd */ 1, /* version number */ pfd_draw_to_window | /* support window */ pfd_doublebuffer | pfd_support_opengl, /* support opengl */ pfd_type_rgba, /* rgba type */ 32, /* 24-bit color depth */ 0, 0, 0, 0, 0, 0, /* color bits ignored */ 0, /* no alpha buffer */ 0, /* shift bit ignored */ 1, 0, 0, 0, 0, /* accum bits ignored */ 24, /* set depth buffer */ 8, /* set stencil buffer */ 0, /* no auxiliary buffer */ pfd_main_plane, /* main layer */ 0, /* reserved */ 0, 0, 0 /* layer masks ignored */ }; attriblist[i++] = wgl_draw_to_window_ext; attriblist[i++] = gl_true; attriblist[i++] = wgl_acceleration_ext; attriblist[i++] = wgl_full_acceleration_ext; attriblist[i++] = wgl_color_bits_ext; attriblist[i++] = 24; attriblist[i++] = wgl_red_bits_ext; attriblist[i++] = 1; attriblist[i++] = wgl_green_bits_ext; attriblist[i++] = 1; attriblist[i++] = wgl_blue_bits_ext; attriblist[i++] = 1; attriblist[i++] = wgl_double_buffer_ext; attriblist[i++] = gl_true; attriblist[i++] = wgl_depth_bits_ext; attriblist[i++] = 1; /* end list */ attriblist[i++] = 0; attriblist[i++] = 0; ppfd = &pfd; if (choosepixelformatarb_func) { gluint numformats; choosepixelformatarb_func(hdc, attriblist, fattriblist, 1, &window->pixelformat, &numformats); } else { /* okay, loaded manually. call gdi functions. */ window->pixelformat = choosepixelformat(hdc, ppfd); } } else { pixelformatdescriptor p; ppfd = &p; describepixelformat(hdc, window->pixelformat, sizeof(pixelformatdescriptor), ppfd); } if (window->pixelformat) { setpixelformat(hdc, window->pixelformat, ppfd); } } break; } return defwindowproc( hwnd, umsg, wparam, lparam ); } dword winapi createwindow(lpvoid args) { windowinfo *window = (windowinfo *)args; hinstance hinstance; wndclass wc; dword window_style; msg msg; char name[32]; handle windowcreated; static int id; sprintf(name, "createwindow%d", window->id); windowcreated = openevent(event_all_access, false, name); windowdestroyed = createevent(null, true, false, null); hinstance = getmodulehandle( null ); if (!hinstance) { printf( "couldn't module handle.\n" ); return gl_false; } if (!getclassinfo(hinstance, "test_window", &wc)) { wc.style = cs_owndc; wc.lpfnwndproc = (wndproc) mainwndproc; wc.cbclsextra = 0; wc.cbwndextra = 0; wc.hinstance = hinstance; wc.hicon = loadicon( null, idi_application ); wc.hcursor = loadcursor( null, idc_arrow ); wc.hbrbackground = (hbrush) getstockobject(black_brush); wc.lpszmenuname = null; wc.lpszclassname = "test_window"; if (!registerclass(&wc)) { printf( "couldn't register window class.\n" ); } } window_style = ws_popup; window->hwnd = createwindowa( "test_window", window_name, window_style, window->x, window->y, window->width, window->height, null, null, hinstance, window); if ( !window->hwnd ) { printf( "create window failed!.\n" ); } window->device_context = getdc(window->hwnd); if (window->visible) { showwindow(window->hwnd, sw_shownormal); } setevent(windowcreated); while (getmessage(&msg, null, 0, 0)) { if (msg.hwnd == window->hwnd) { translatemessage(&msg); dispatchmessage(&msg); } } closehandle(windowcreated); window->hwnd = null; window->device_context = 0; setevent(windowdestroyed); return 0; } glboolean createtestwindow(windowinfo *window ) { handle windowcreated; char name[32]; sprintf(name, "createwindow%d", window->id); printf("create window %d\n", window->id); windowcreated = createevent(null, true, false, name); createthread(null, 2000, createwindow, (lpvoid)window, 0, null); waitforsingleobject(windowcreated, 50000); closehandle(windowcreated); printf("window created\n"); return gl_true; } int _tmain(int argc, _tchar* argv[]) { hwnd hwnd; windowinfo window[34]; char windowtitle[34]; int i; static int id = 1; // create window window[0].id = 1; window[0].x = 40; window[0].y = 600; window[0].width = 800; window[0].height = 600; window[0].visible = 0; // wait make window visible window[0].pixelformat = 9; window[0].title = _strdup("window 0"); createtestwindow(&window[0] ); // create context // window[0].hglrc = wglcreatecontext((hdc)window[0].device_context ); // wglmakecurrent(window[0].device_context, window[0].hglrc); // load function pointers // choosepixelformatarb_func = // (choosepixelformatarbfunc_t)wglgetprocaddress("wglchoosepixelformatarb"); // destroy context // wglmakecurrent(window[0].device_context, 0); // wgldeletecontext(window[0].hglrc); // deletedc(window[0].device_context); // destroy window postmessage(window[0].hwnd, wm_quit, 0, 0); free(window[0].title); while (1) { (i = 0; < 34; i++) { // create 34 windows window[i].id = + 1; window[i].x = 40; window[i].y = 600; window[i].width = 800; window[i].height = 600; window[i].visible = 1; // wait make window visible window[i].pixelformat = 9; sprintf(windowtitle, "window %d", window[i].id); window[i].title = _strdup(windowtitle); createtestwindow(&window[i] ); // create 34 contexts // window[i].hglrc = wglcreatecontext((hdc)window[i].device_context ); // wglmakecurrent(window[i].device_context, window[i].hglrc); } sleep(3000); (i = 0; < 34; i++) { // destroy contexts // wglmakecurrent(window[i].device_context, 0); // wgldeletecontext(window[i].hglrc); // deletedc(window[i].device_context); // destroy windows postmessage(window[i].hwnd, wm_quit, 0, 0); sleep(100); printf("%s destroyed\n", window[i].title); free(window[i].title); } sleep(2000); } return 0; }
Comments
Post a Comment