MFC应用程序使用C#COM DLL发送电子邮件,观察到巨大的内存泄漏

时间:2018-03-06 08:41:26

标签: c# visual-c++ com

.net memory profiler report, which shows smtpclient instances not GCed这是VC ++代码,它使用C#COM DLL发送消息。 由于我想发送多封电子邮件,因此多次执行此代码。 每次通话,我都会看到任务管理器中的内存增加,而且它永远不会再次出现......

void CTestMailLibraryDlg::OnOK() 
{
CoInitialize(NULL);

HRESULT hr;
IMailLibraryPtr  pLib;
hr = pLib.CreateInstance(CLSID_MailSetup);

if(pLib != NULL)
{
    BSTR server = SysAllocString(L"smtp.gmail.com"); 
    pLib->MailServerSetup(server);
    SysFreeString(server);

    BSTR To = SysAllocString(L"rahulppawar22@gmail.com"); 
    pLib->AddToRecipient(To);
    SysFreeString(To);

    BSTR from = SysAllocString(L"apogeeinsight1@gmail.com"); 
    BSTR subject = SysAllocString(L"subject"); 
    BSTR msg = SysAllocString(L"msg"); 
    BSTR username = SysAllocString(L"apogeeinsight1@gmail.com"); 
    BSTR password = SysAllocString(L"Apogee$1234"); 
    BSTR attachment = SysAllocString(L""); 

    pLib->SendMail(from,subject,msg,attachment,587,username,password,1);

    SysFreeString(from);
    SysFreeString(subject);
    SysFreeString(msg);
    SysFreeString(username);
    SysFreeString(password);
    SysFreeString(attachment);
}

CoUninitialize();
}

这是C#COM DLL代码。以下发送功能在每次通话时被多次调用,内存堆积起来并且永远不会再次释放..

namespace MailLibrary
{
[ComVisible(true), GuidAttribute("FA1DD41F-1547-4A38-BE20-1BFE28EAF87C")]
public class MailSetup : IDisposable, IMailLibrary
{
    MailMessage _mail;
    SmtpClient _smtpobj;
    public void MailServerSetup(string serverName)
    {
        _mail = new MailMessage();
        _smtpobj = new SmtpClient(serverName);
    }

    public void AddToRecipient(string toAddress)
    {
        _mail.To.Add(toAddress);
    }

    public void AddCcRecipient(string ccAddress)
    {
        _mail.CC.Add(ccAddress);
    }

    public void AddBccRecipient(string bccAddress)
    {
        _mail.Bcc.Add(bccAddress);
    }

    public string SendMail(string fromAddress,string subject, string body, string strAttachment, int port,string username, string password, bool bSsl)
    {
        string strReturn = "";
        try
        {
            _mail.From = new MailAddress(fromAddress);
            _mail.Subject = subject;
            _mail.Body = body;
            if (strAttachment != "")
            {
                Attachment attachment = new Attachment(strAttachment);
                _mail.Attachments.Add(attachment);
            }
            _smtpobj.Port = port;
            _smtpobj.Credentials = new System.Net.NetworkCredential(username, password);
            _smtpobj.EnableSsl = bSsl;
            _smtpobj.Send(_mail);
            Cleanup(); 

        }
        catch (Exception ex)
        {
            Cleanup();
            strReturn = ex.ToString();
        }
        return strReturn;
    }

    public void Cleanup()
    {
        _smtpobj.ServicePoint.CloseConnectionGroup(_smtpobj.ServicePoint.ConnectionName);
        _mail.Dispose();
        _smtpobj.Dispose();
       Debug.Write("RP:Cleanup Done");
    }

    public virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            Dispose();
        }
    }
    public void Dispose()
    {
        this.Dispose(true);
        Cleanup();
        GC.SuppressFinalize(this);
    }
}

}

0 个答案:

没有答案