Hi Youlong,

I would suspect your usage of COM. Instead of using a CComBSTR, try any
array of OLECHAR.

The following looks like a nice reference:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html
/vcconprogrammingwithccombstr.asp

Note the following from the above (I see your using the m_str member):

Memory Leak Issues
Passing the address of an initialized CComBSTR to a function as an [out]
parameter causes a memory leak.

...


> I wonder why I have so many problems
Par for the course.

Jeffrey Walton

-----Original Message-----
From: 曹玉龙 [mailto:[EMAIL PROTECTED] 
Sent: Thursday, December 02, 2004 12:40 AM
To: Walton, Jeffrey (Contractor)
Subject: memory leak in AES encryption

Hi, Jeff

        I am so sorry. It took so much your time to help me to solve
problems. But I could not send mails to [EMAIL PROTECTED]
net. Every times he replied "Your mail to 'Cryptopp-announce' Is being held
until the list moderator can review it for approval."

        So I have to still send mails to you. I wonder why I have so many
problems ^_^

        I wrote a function to encrypt BSTR with AES algorithm. While calling
this function, I found memory leak.
        I repeatedly called this function for 1000 times, the length of
plain if 512KB, about 130MB memory was taked by my application. It may be a
horrible news.
        Does anybody could give me some advices. Thank you

        The following is my code.


STDMETHODIMP CAESFunc::Encrypt(BSTR sPlain, BSTR *sCipher)
{
        CComBSTR *strTemp=new CComBSTR();
        byte *pBuffer, *pOutBuf;
        int iLen;

        strTemp->Empty();
        strTemp->AppendBSTR(sPlain);
        pBuffer = (byte*)malloc(strTemp->Length()<<1);
        iLen = WideCharToMultiByte(CP_ACP, NULL, strTemp->m_str, 
                strTemp->Length(), (char*)pBuffer, strTemp->Length()<<1,
NULL, NULL);

        CBC_Mode<AES>::Encryption myAES(m_AESKey, m_iKeySize, m_AES_IV);
        StreamTransformationFilter filterEnc(myAES, 0,
StreamTransformationFilter::PKCS_PADDING);
        filterEnc.Put((byte*)pBuffer, iLen);
        filterEnc.MessageEnd();

        pOutBuf = (byte*)malloc(iLen + 16);
        iLen = filterEnc.Get(pOutBuf, iLen + 16);
        Base64Encoder objCoder;
        objCoder.Put(pOutBuf, iLen);
        objCoder.MessageEnd();
        free(pBuffer);
        pBuffer = (byte*)malloc(iLen*2);
        iLen = objCoder.Get(pBuffer, iLen*2);

        free(pOutBuf);
        pOutBuf = (byte*)malloc(iLen * 2);
        iLen = mbstowcs((unsigned short*)(pOutBuf), (const char*)pBuffer,
iLen);
        strTemp->Empty();
        strTemp->Append((unsigned short*)pOutBuf, iLen);
        *sCipher = strTemp->Copy();
        free(strTemp);
        free(pBuffer);
        free(pOutBuf);

        return S_OK;
}

Yulong Cao



Reply via email to