GDI对象泄漏检查的一点经验
程序出现GDI泄漏,在GDIndicator中,Total字段的两个值不一致的情况,前面比后面大了很多,一直没有找到为何,以为是没释放就会导致不一致;
查看了程序代码,一直无法找到泄漏的地方,于是找各种工具来查看究竟那里泄漏,直到使用 GDIView 的说明里面:
'GDI Total' and 'All GDI' columns
The 'GDI Total' column (a new column added on v1.05) displays the total number of all GDI handles found in the GDI table for the specified process. This means that 'GDI Total' column display the sum of the following columns: Pen, ExtPen, Brush, Bitmap, Font, Palette, Region, DC, Metafile DC, Enhanced Metafile DC, and Other GDI.
The 'All GDI' value is taken from Windows API call (GetGuiResources), and it usually contains a value larger than 'GDI Total', probably because it also counts some internal kernel GDI objects that are not included in the GDI objects table of the process.
Notice: If you have a problem that the 'All GDI' value is increased, while there is no leak with the other GDI values, it means that you probably have a leak in the creation of icons or cursors (Icons and cursors are created without destroying them later).
于是查找HICON,HCURSOR的地方,发现有个释放HICON的函数,使用的是 DeleteObject ,而不是 DestroyIcon,修改之后,GDI泄漏有所缓解。
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步