从窗口的额外字节中泄漏内存?
我正在创建一个窗口 C++ 类,并且在注册窗口类时为创建的每个窗口使用额外的字节(使用 WNDCLASSEX 结构的 cbWndExtra 等)。 ::DestroyWindow(..) 是否释放分配的窗口的额外字节?
似乎当我在循环中创建和销毁许多窗口对象时,任务管理器中我的应用程序的“提交大小”会上升。
我的 C++ 类不分配任何内存,而且它上面没有控件/工具栏/菜单,它似乎没有泄漏任何 GDI 对象或类似的东西,所以我怀疑它是窗口的额外字节的东西。
有人对可能出什么问题有任何想法吗?
我应该调用一些 API 来释放额外的字节吗?
销毁使用额外字节的窗口时还必须执行其他操作吗?
编辑: 我确实尝试仅创建特定类的单个窗口,该窗口使用额外的字节并在循环中销毁它,并且我的应用程序的提交大小再次增加。我也等了几个小时,提交大小根本没有减少。 额外的字节仅包含指向表示窗口的对象的指针。该对象被销毁(它是在循环中静态创建的)。 无论如何,这似乎不是我的错,而且它甚至可能不是 Windows 的错误(正如你所说 - 虽然我不是 100% 确定),所以我打算将其保留原样......
I am making a window C++ class and I am using extra bytes for each window created (using the cbWndExtra of the WNDCLASSEX struct, etc) when registering the window class.
Does ::DestroyWindow(..) release the allocated window's extra bytes?
It seems that when I create and destroy many window objects in a loop the "Commit Size" of my app in Task Manager rises.
My C++ class does not allocate any memory, moreover it has no controls/toolbars/menus on it, it does not seem to leak any GDI objects or anything like that, so I suspect it is something with the extra bytes of the window.
Does anyone have any ideas on what can be going wrong?
Is there some API I should call to release the extra bytes?
Is there something else you must do when destroying a window that is using extra bytes?
EDIT:
I did try creating just a single window of the specific class that uses the extra bytes and destroying it in a loop and, again, the commit size of my app rises. I also waited for a few hours and the the commit size didn't decrease at all.
The extra bytes contain only a pointer to the object that represents the window. This object gets destroyed (it is created statically in the loop).
Anyway, it seems that it's not my fault and that it might not even be a bug of Windows (as you say - although I'm not 100% sure) so I'm about to leave it as is...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,它确实释放了额外的字节。也许您在其中存储了指向已分配内存的指针,这就是泄漏的原因。
此外,“提交大小”没有下降并不能证明存在内存泄漏。分配器不会立即将释放的内存返回给操作系统,它们可以保留它并在以后重用。尝试创建一个简单的 CreateWindowEx/DestroyWindow 无限循环来验证。
Yes, it does release the extra bytes. Maybe you're storing pointers to allocated memory in them, and that's what's leaking.
Also, “Commit size” not going down does not prove a memory leak. Allocators don't immediately return freed memory to the OS, they can keep it and reuse later. Try creating a simple CreateWindowEx/DestroyWindow infinite loop to verify.
您的工作是通过设置 cbWndExtra 字段来释放您引用的任何内存。如果您考虑一下,这是有道理的,因为 Windows 不知道您如何分配内存(例如 malloc、new、LocalAlloc 等)
编辑:因为我投了反对票,也许我不清楚。如果您将指针插入该字段,并且它指向您在应用程序中分配的内容,则该内存将不会被释放。额外的字节将会是,但不是它们所指向的。
It's your job to release any memory you reference by setting the cbWndExtra field. If you think about it this makes sense since Windows has no knowledge of how you allocated the memory in the first place (eg malloc, new, LocalAlloc etc)
EDIT: Since I've had a downvote maybe I wasn't being clear. If you stick a pointer into the field and it points to something you've allocated in your app then that memory won't be released. The extra bytes will be, but not what they point to.