Win32 API资源分配释放速查,防止代码资源泄露

 

下面为某次浏览找到的Win32资源的分配清除文档,只简单的翻译了几个文字。该

文档对防止资源泄露有很大帮助。抱歉一贴上就串了。

***********************************************************************

资源                     分配函数                                   清除函数              需要手动清除?

======================================================================

 

Accelerators       LoadAccelerators                         N/A                  No

 

Atoms               GlobalAddAtom                        GlobalFreeAtom       Yes

 

Bitmaps                CreateBitmap                      DeleteObject             Yes *

                CreateBitmapIndirect                   DeleteObject             Yes *

                CreateCompatibleBitmap           DeleteObject                Yes *

                CreateDIBitmap                   DeleteObject       Yes *

                CreateDiscardableBitmap               DeleteObject                Yes *

                LoadBitmap                       DeleteObject                Yes *

 

Brushes         CreateBrushIndirect                   DeleteObject         Yes

                CreateDIBPatternBrush            DeleteObject         Yes

                CreateHatchBrush                 DeleteObject         Yes

                CreatePatternBrush                   DeleteObject         Yes

                CreateSolidBrush                 DeleteObject         Yes

 

Carets                 CreateCaret                      DestroyCaret         No  #

 

Clipboard              OpenClipboard                    CloseClipboard       Yes

 

Comms Ports     OpenComm                  CloseComm          Yes

 

Cursor                 CreateCursor                     DestroyCursor           Yes

                LoadCursor                       N/A                 No

 

DC             CreateDC                         DeleteDC             Yes

                CreateCompatibleDC               DeleteDC             Yes @

                GetDC                            ReleaseDC            Yes

                GetWindowDC                      ReleaseDC            Yes

                BeginPaint                       EndPaint             Yes

 

Dialog Box             CreateDialog                     DestroyWindow        No

                CreateDialogIndirect                  DestroyWindow        No

                CreateDialogIndirectParam             DestroyWindow        No

                CreateDialogParam                DestroyWindow        No

 

File                   OpenFile                         _lclose                     No

 

Fonts                  CreateFont                       DeleteObject         Yes

                CreateFontIndirect                   DeleteObject                Yes

                AddFontResource                  RemoveFontResource         Yes

 

GDI Stock Obj          GetStockObject                   N/A                  No  &

 

Hook                  SetWindowsHook                   UnhookWindowsHook    Yes

 

IC              CreateIC                         DeleteDC             Yes

 

Icon            CreateIcon                       DestroyIcon          Yes

                LoadIcon                         N/A                  No

 

Library                LoadLibrary                      FreeLibrary          Yes

 

Memory         AllocDStoCSAlias                 FreeSelector         Yes

                AllocSelector                           FreeSelector         Yes

                GlobalAlloc                      GlobalFree           No

                GlobalDOSAlloc                   GlobalDOSFr          No

                GlobalFix                        GlobalUnfix          No

                GlobalLock                       GlobalUnlock         No

                GlobalPageLock                   GlobalPageUnlock     No

                GlobalRealloc                    GlobalFree           No

                GlobalWire                       GlobalUnwire         No

                LocalAlloc                       LocalFree            No

                LocalLock                        LocalUnlock          No

                LocalRealloc                     LocalFree            No

 

Menu           CreateMenu                       DestroyMenu          No  %

                CreatePopupMenu                  DestroyMenu          No  %

                LoadMenu                         DestroyMenu          No  %

                LoadMenuIndirect                 DestroyMenu          No  %

                GetSystemMenu                    N/A                  No

 

MetaFile               CreateMetaFile                   DeleteMetaFile       Yes

 

Palette                CreatePalette                           DeleteObject         Yes

 

Pen             CreatePen                        DeleteObject         Yes

                CreatePenIndirect                   DeleteObject         Yes

 

Regions         CreateEllipticRgn                    DeleteObject         Yes

                CreateEllipticRgnIndirect               DeleteObject         Yes

                CreatePolygonRgn                 DeleteObject         Yes

                CreatePolyPolygonRgn             DeleteObject         Yes

                CreateRectRgn                    DeleteObject         Yes

                CreateRectRgnIndirect               DeleteObject         Yes

                CreateRoundRectRgn               DeleteObject         Yes

 

Resource               AllocResource                    FreeResource         No

                FindResource                     N/A                  No

                LoadResource                     FreeResource         No

                LockResource                     UnlockResource       No

 

Sound                 OpenSound                        CloseSound           Yes

 

String                 LoadString                       N/A                  No

 

Thunk                 MakeProcInstance                 FreeProcInstance     No

 

Timer           SetTimer                         KillTimer            No

 

Window         CreateWindow                     DestroyWindow        No

                CreateWindowEx                   DestroyWindow        No

 

 

Notes:

------

*   位图资源在释放的时候必须从选中它的DC中脱离。

%   当菜单失去焦点和窗口不再关联或者应用程序结束的时候占用资源必须被释放。

@   DC被释放前必须和它关联(选中)的位图取消联系

#   如果脱字符指定了位图,位图必须被释放

&   DeleteObject的调用不会销毁常规的StockObject(系统内定资源)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Win32 API 是一组用于编写 Windows 应用程序的函数和结构。使用 Win32 API 可以创建迷宫小游戏应用程序代码,以下是一个简单的例子: ```c #include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT("MazeGame"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppName, TEXT("Maze Game"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; switch (message) { case WM_PAINT: hdc = BeginPaint(hwnd, &ps); // 在这里绘制迷宫和游戏元素 EndPaint(hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); } ``` 以上代码Win32 API 编写的迷宫小游戏应用程序代码的简单示例。通过注册窗口类、创建窗口、消息循环和窗口过程,可以实现基本的窗口绘制和交互功能,同时开发者还需要根据具体需求添加迷宫的逻辑和游戏元素的绘制与交互处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值