gpt4 book ai didi

c++ - 如何正确使用 LoadImage 和 DeleteObject?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:32:44 25 4
gpt4 key购买 nike

我正在使用 C++ 开发 Windows 应用程序。我使用 LoadImage 将 bmp 文件加载到 DC,它正确显示。但是,当我调用 DeleteObject 时,内存似乎并没有被释放。 (我使用 windows 任务管理器来跟踪内存使用情况)

在 WM_INITDIALOG 部分,我这样做:

static HBITMAP hBitmap = 0;
char* tempPath = "tabView.bmp";
hBitmap = (HBITMAP)LoadImage(NULL,
tempPath, // file containing bitmap
IMAGE_BITMAP, // type = bitmap
0, 0, // original size
LR_LOADFROMFILE); // get image from a file
if(hBitmap)
{
SendMessage(GetDlgItem(hwndDlg, IDC_PICTURE),
STM_SETIMAGE, // message to send
(WPARAM)IMAGE_BITMAP, // bitmap type
(LPARAM)hBitmap); // bitmap handle
}

所以图片出现在DC中,内存增加。在一个按钮中我做了:

int result = DeleteObject(hBitmap);

当我按下按钮时,我检查了结果,它是一个非零值,这就是成功。但 IDC_PICTURE 仍会显示图片,内存保持不变。我想知道 SendMessage() 是否会增加 hBitmap 上的引用计数...

所以我的问题是:什么是正确的清理方法?

最佳答案

您没有提到您使用的是哪个版本的 Windows。无论如何,如果您阅读了 STM_SETIMAGE 的“重要”部分, 你会看到下一个:

With Windows XP, if the bitmap passed in the STM_SETIMAGE message contains pixels with nonzero alpha, the static control takes a copy of the bitmap. This copied bitmap is returned by the next STM_SETIMAGE message. The client code may independently track the bitmaps passed to the static control, but if it does not check and release the bitmaps returned from STM_SETIMAGE messages, the bitmaps are leaked.

也许这不仅适用于 Windows XP,也适用于更高版本的 Windows。希望这对您有所帮助。

关于c++ - 如何正确使用 LoadImage 和 DeleteObject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10112470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com