从 CWnd::FromHandle 获得的 CWnd 的生命周期是多少?
根据 msdn,当我得到 CWnd 时* 与 CWnd::FromHandle 一起,
指针可能是临时的,不应存储以供以后使用。
我不清楚“稍后使用”是什么意思。它只是当前方法的范围吗? 据我所知,Win32中没有GC!
According tomsdn, when I get a CWnd* with CWnd::FromHandle,
The pointer may be temporary and should not be stored for later use.
What is meant by "later use" is not clear to me. Is it only the scope of the current method?
As far as I know, there is no GC in Win32!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
MFC维护了许多句柄映射,从HWND到CWnd,HDC到CDC等,它们存储在线程状态中。每个句柄映射都包含一个永久映射和临时映射 - 当您调用 CWnd::Create 或 CDC::Attach 等方法时,会添加永久条目,而当您在没有句柄的句柄上调用 FromHandle 时,会创建临时条目。永久入境。
临时条目在空闲处理期间(在 CWinApp::OnIdle 中)被清除,因此只能在处理当前消息时安全地使用它们。一旦您返回到消息循环,或进入另一个模态循环(例如通过调用DoModal),它们就可能被删除。
MFC maintains a number of handle maps, from HWND to CWnd, HDC to CDC etc, which are stored in the thread state. Each handle map contains a permanent map and temporary map - permanent entries are added when you call a method such as CWnd::Create or CDC::Attach, while temporary entries are created when you call FromHandle on a handle that doesn't have a permanent entry.
Temporary entries are cleaned up during idle processing (in CWinApp::OnIdle), so they can only safely be used while processing the current message. As soon as you return to the message loop, or enter another modal loop (e.g. by calling DoModal) then they may be deleted.
基于相同的 MSDN 描述,我假设这意味着如果没有 CWnd 附加到作为对象提供的 hWnd,它将创建一个临时 CWnd,一旦超出范围或调用其他地方的析构函数,该临时 CWnd 可能会被销毁,或者为相关 hWnd 显式创建 CWnd。因此,如果您已经创建了 CWnd,那么应该没问题,否则您可能需要非常小心地存储收到的指针。
Based on the same MSDN description, I would assume that this means that if no CWnd is attached to the hWnd provided as an object, it will create a temporary CWnd which probably gets destroyed once something goes out of scope, or a destructor elsewhere is called, or a CWnd is explicitly created for the hWnd in question. So if you already have a CWnd created, you should be OK, otherwise you will probably need to be very careful with storing the pointer you receive.
通常他们只希望您在函数范围内使用此句柄。不要将其存储为类字段,在对象的整个生命周期中引用它。
Typically they only want you to use this handle with in the scope of your function. And not to store it as a class field where you reference it through out the life of your object.
FromHandle 基本上用于获取对已经存在的窗口对象的瞬时引用。 MFC 将这些引用存储在称为临时句柄映射的内部结构中(句柄映射是 Windows HWND 到 MFC CWnd 对象的映射,MFC 使用该映射进行 Win32 调用来操作 MFC 对象对应的实际 Windows 窗口)。为了避免此结构中的对象数量增长超出所有范围,在 MFC 的空闲循环处理期间从句柄映射中删除项目。
正如您可能已经猜到的,还有一个永久句柄映射不会有这种自动清理行为。如果您需要获取不将其 HWND 引用放入临时句柄映射中的 CWnd 对象,您可以调用 FromHandlePermanent()。
-罗恩
FromHandle is basically used for getting a transient reference to an already existing window object. MFC stores these references in an internal structure called a temporary handle map (a handle map is a map of Windows HWNDs to MFC CWnd objects used by MFC to make Win32 calls to manipulate the actual Windows window the MFC object corresponds to). In order to avoid the number of objects in this structure from growing beyond all bounds, items are deleted from the handle map during MFC's idle loop processing.
As you may have guessed, there is also a permanent handle map that won't have this automatic clean up behavior. If you need to get a CWnd object that doesn't put its HWND reference in the temporary handle map you can call FromHandlePermanent().
-Ron