简体   繁体   English

在CRichEditCtrl上使用表情符号时内存泄漏

[英]Memory leak while using emoticons on CRichEditCtrl

I'm developing a text editor class (for a chat application) based on CRichEditCtrl (MFC) with emoticon support. 我正在开发一个基于CRichEditCtrl (MFC)并带有表情符号支持的文本编辑器类(用于聊天应用程序)。

After I load the emoticon's bitmap, I use the function OleCreateStaticFromData to insert it into CRichEditCtrl . 加载表情符号的位图后,我使用OleCreateStaticFromData函数将其插入到CRichEditCtrl After that I just delete the bitmap object allocated by myself. 之后我只删除自己分配的位图对象。 I can verify (using a GDIView utility) that all resources I allocate have been properly released. 我可以验证(使用GDIView实用程序)我分配的所有资源都已正确发布。

This works perfectly: the bitmap (emoticon) is drawn on the CRichEditCtrl window and is handled just like a character. 这非常有效:位图(表情符号)在CRichEditCtrl窗口上绘制,并且像字符一样处理。

My problem is that I don't know how to deallocate the memory (internal) allocated by OleCreateStaticFromData to manage the bitmap (emoticon). 我的问题是我不知道如何释放由OleCreateStaticFromData分配的内存(内部)来管理位图(表情符号)。 The memory allocated for any emoticon used is never released, even if I delete the CRichEditCtrl object. 即使我删除了CRichEditCtrl对象,也不会释放为所使用的任何表情符号分配的内存。 I'd like to know how to fix that issue. 我想知道如何解决这个问题。 Is that a MFC's issue or I'm doing something wrong ? 这是MFC的问题还是我做错了什么?

OleCreateStaticFromData returns a COM pointer which you pass to the Rich Edit control. OleCreateStaticFromData返回一个COM指针,您将其传递给Rich Edit控件。 This COM pointer has had an AddRef applied to it, so you must call Release once you have passed it to the control, to signify that you no longer will be accessing it directly. 此COM指针已应用AddRef ,因此您必须在将其传递给控件后调用Release ,以表示您不再直接访问它。 The control will also do a Release when it is destroyed which should result in the COM object being deleted. 控件在销毁时也会执行Release ,这会导致COM对象被删除。

You might also store the returned pointer in a smart pointer class _com_ptr_t or CComPtr and it will Release it automatically. 您还可以将返回的指针存储在智能指针类_com_ptr_tCComPtr ,它将自动Release它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM