HBITMP 的 WIndows 函数 DestroyObject 不能很好地工作(内存泄漏)

发布于 2024-11-08 00:08:56 字数 632 浏览 3 评论 0原文

我需要经常在C++程序中创建HBITMAP图像,当然我需要在使用后删除这些位图。 代码与此类似,

HBITMAP hBmp;
while(true) {
  hBmp = CreateBitmap(width, height, 1, 8, imageData);
  process(hBmp);
  DeleteObject(hBmp);
}

我在一个线程中有一个无限循环,不断创建 HBITMAP,调用使用该位图的函数,然后删除它。在循环开始时,我检查进程内存使用量是否大于前一个循环,如果是,则打印它。 使用CreateBitmap() 和DeleteObject() 会导致少量内存泄漏;进程内存使用量偶尔会增加 4KB(有时每 10 秒一次,有时几分钟没有任何反应)。

我也没有调用 process 函数进行了测试,问题仍然存在,所以我认为是由于位图处理造成的。 此外,我做了另一项测试,在无限循环之外创建图像(因此我只创建一次)并在循环中无限次处理它,并且没有发生内存泄漏。

注意:DeleteObject() 始终返回大于 0 的值(无错误)。

该问题是否可能与 DeleteObject() 函数有关?以这种方式创建/删除位图有问题吗?

技术说明: 视窗XP Borland C++ Builder 5

I need to create HBITMAP images very often in a C++ program, and of course I need to delete these bitmap after use.
The code is similar to this

HBITMAP hBmp;
while(true) {
  hBmp = CreateBitmap(width, height, 1, 8, imageData);
  process(hBmp);
  DeleteObject(hBmp);
}

I have an infinite cycle in a thread that continuously create an HBITMAP, call a function that use this bitmap, and then delete it. At loop start, I check if the process memory usage is bigger than the previous cycle, and if it is, I print it.
Using CreateBitmap() and DeleteObject() result in a small memory leak; the process memory usage increase by 4KB once in a while (sometimes every 10 seconds, sometimes nothing happens for minutes).

I tested it without calling the process function, too, and the problem is still there, so I think is due to the bitmap handling.
Furthermore, I've made another test, creating the image outside the infinite loop (so I create it just one time) and processing it infite times in the loop, and no memory leak occures.

NOTE: DeleteObject() always return a value >0 (no errors).

Is it possible that the problem is related to DeleteObject() function? Is there something wrong in creating/deleting bitmap in this way?

TECH NOTES:
Windows XP
Borland C++ Builder 5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

信愁 2024-11-15 00:08:57

是否有可能在设备上下文中的某处仍然选择位图?这可能会导致 DeleteObject 失败,尽管我希望它返回错误。

Is there any possibility that the bitmap is still selected in a device context somewhere? That might cause the DeleteObject to fail, although I'd expect it to return an error.

鸢与 2024-11-15 00:08:57

有趣的是,问题通常出现在人们显示的代码中。

在您的 process 函数中,您在设备上下文中选择位图,但不会再次选择它,因此当您尝试删除它时,它仍然是设备上下文的一部分。

顺便说一句,您的上下文也没有正确释放,您选择的所有其他对象也没有正确释放。那里发生了一些巨大的内存泄漏。当然,您没有向我们展示代码,因此无法帮助您了解具体细节。

最后一点,我不明白一遍又一遍地创建新位图只是为了发布它背后的原因。您应该在循环外部创建它,然后在设备上下文中创建它,然后将其选择到设备上下文中,然后启动循环并清除位图以开始在其上写入。您将获得巨大的性能提升。

Funny how the problem is usually in the code people don't show.

Inside your process function you're selecting your bitmap within a device context, but you don't select it back out again, so when you try to delete it it's still part of a device context.

As an aside, your context isn't properly releasing either, nor are all the other objects you select into it. You have some huge memory leaks going on there. Of course you don't show us the code so can't help you with specifics.

And as a final point, I don't exactly see the reason behind creating a new bitmap over and over just to release it. You should create it outside the loop, then the device context, then select it into the device context, and just then you start your loop and clear the bitmap to begin writing on it. You'll get a huge performance increase.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文