Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leak while using emoticons on CRichEditCtrl

Tags:

c++

c

windows

mfc

I'm developing a text editor class (for a chat application) based on CRichEditCtrl (MFC) with emoticon support.

After I load the emoticon's bitmap, I use the function OleCreateStaticFromData to insert it into 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.

This works perfectly: the bitmap (emoticon) is drawn on the CRichEditCtrl window and is handled just like a character.

My problem is that I don't know how to deallocate the memory (internal) allocated by OleCreateStaticFromData to manage the bitmap (emoticon). The memory allocated for any emoticon used is never released, even if I delete the CRichEditCtrl object. I'd like to know how to fix that issue. Is that a MFC's issue or I'm doing something wrong ?

like image 477
Jorg B Jorge Avatar asked May 31 '10 22:05

Jorg B Jorge


1 Answers

OleCreateStaticFromData returns a COM pointer which you pass to the Rich Edit control. 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. The control will also do a Release when it is destroyed which should result in the COM object being deleted.

You might also store the returned pointer in a smart pointer class _com_ptr_t or CComPtr and it will Release it automatically.

like image 120
Mark Ransom Avatar answered Oct 26 '22 14:10

Mark Ransom