起因是我在ocx中封裝了一個(gè) richedit2.0 控件,并自己寫了一個(gè)
用于顯示圖片
ole 對(duì)象 。在我的ocx中插入大量的ole對(duì)象,大約3000個(gè),然后保存為rtf。再使用我的ocx控件打開這個(gè)rtf時(shí),會(huì)提示 內(nèi)存不足。跟蹤代碼時(shí)發(fā)現(xiàn)在我實(shí)現(xiàn)的 IRichEditOleCallback 接口的 GetNewStorage(LPSTORAGE* lplpstg) 中報(bào)錯(cuò)。
我的程序中創(chuàng)建 IStorage的思路是,在 IRichEditOleCallback 接口初始化時(shí)調(diào)用 StgCreateDocfile 創(chuàng)建一個(gè)根 Istorage,以后每個(gè)對(duì)象插入的時(shí)候 IRichEditOleCallback 的 GetNewStorage 接口被調(diào)用,在
GetNewStorage
方法中 在 根 Istorage 下建立一個(gè)subStroage。
HRESULT hResult = ::StgCreateDocfile(NULL,STGM_TRANSACTED|STGM_READWRITE | STGM_SHARE_EXCLUSIVE |STGM_CREATE ,
0, &m_pStorage );
//建立一個(gè)sub storage
WCHAR tName[50] = {0};
swprintf(tName, L"substorage%d", m_iNumStorages);
HRESULT hResult = pStorage->CreateStorage(tName,
STGM_TRANSACTED|STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE ,
0, 0, lplpstg );
后來發(fā)現(xiàn) 把 STGM_TRANSACTED 標(biāo)識(shí)去掉就可以了。有點(diǎn)莫名其妙,估計(jì)是事務(wù)的處理會(huì)導(dǎo)致內(nèi)存的占用增加的緣故吧。
另外 在msdn 中 搜索 STGM ,注意紅色的字體,也就是收 使用com中的 IStorage 接口,是不能創(chuàng)建具有 STGM_TRANSACTED 表示的 IStream對(duì)象的
- STGM_TRANSACTED
- In transacted mode, changes are buffered and written only if an explicit
commit operation is called. To ignore the changes, call the Revert method
in the IStream, IStorage, or IPropertyStorage interface.
The COM compound file implementation of IStorage does not support
transacted streams, which means that streams can be opened only in direct mode,
and you cannot revert changes to them, however transacted storages are
supported. The compound file, stand-alone, and NTFS file system implementations
of IPropertySetStorage similarly do not support transacted, simple
property sets because these property sets are stored in streams. However,
transactioning of nonsimple property sets, which can be created by specifying
the PROPSETFLAG_NONSIMPLE flag in the grfFlags parameter of
IPropertySetStorage::Create, are supported.