Index: crashrpt/CrashRpt.vcproj
===================================================================
--- crashrpt/CrashRpt.vcproj	(revision 17027)
+++ crashrpt/CrashRpt.vcproj	(working copy)
@@ -55,7 +55,7 @@
 				Optimization="1"
 				InlineFunctionExpansion="1"
 				FavorSizeOrSpeed="2"
-				AdditionalIncludeDirectories=".\zlib;..\crashrpt"
+				AdditionalIncludeDirectories=".\zlib;.;..\Utils"
 				PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;_CRT_SECURE_NO_DEPRECATE"
 				StringPooling="false"
 				ExceptionHandling="1"
Index: crashrpt/MailMsg.cpp
===================================================================
--- crashrpt/MailMsg.cpp	(revision 17027)
+++ crashrpt/MailMsg.cpp	(working copy)
@@ -10,6 +10,7 @@
 
 #include "stdafx.h"
 #include "MailMsg.h"
+#include "auto_buffer.h"
 
 CMailMsg::CMailMsg()
 {
@@ -32,7 +33,7 @@
 
 CMailMsg& CMailMsg::SetFrom(string sAddress, string sName)
 {
-   if (m_bReady || Initialize())
+   if (initIfNeeded())
    {
       // only one sender allowed
       if (m_from.size())
@@ -46,7 +47,7 @@
 
 CMailMsg& CMailMsg::SetTo(string sAddress, string sName)
 {
-   if (m_bReady || Initialize())
+   if (initIfNeeded())
    {
       // only one recipient allowed
       if (m_to.size())
@@ -60,7 +61,7 @@
 
 CMailMsg& CMailMsg::SetCc(string sAddress, string sName)
 {
-   if (m_bReady || Initialize())
+   if (initIfNeeded())
    {
       m_cc.push_back(TStrStrPair(sAddress,sName));
    }
@@ -70,7 +71,7 @@
 
 CMailMsg& CMailMsg::SetBc(string sAddress, string sName)
 {
-   if (m_bReady || Initialize())
+   if (initIfNeeded())
    {
 	   m_bcc.push_back(TStrStrPair(sAddress, sName));
    }
@@ -80,7 +81,7 @@
 
 CMailMsg& CMailMsg::AddAttachment(string sAttachment, string sTitle)
 {
-   if (m_bReady || Initialize())
+   if (initIfNeeded())
    {
       m_attachments.push_back(TStrStrPair(sAttachment, sTitle));
    }
@@ -91,7 +92,7 @@
 BOOL CMailMsg::Send()
 {
    // try mapi
-   int status = MAPISend();
+   const int status = MAPISend();
    if (status != 0)
       return status == 1;
    // try cmc
@@ -120,110 +121,105 @@
 */
 int CMailMsg::cResolveName( LHANDLE m_lhSession, const char * lpszName, lpMapiRecipDesc *ppRecip )
 {	
-	HRESULT hRes = E_FAIL;
+	// Always check to make sure there is an active session
+	if (!m_lhSession)
+	{
+		return E_FAIL;
+	}
+
 	FLAGS flFlags = 0L;
 	ULONG ulReserved = 0L;
 	lpMapiRecipDesc pRecips = NULL;
-	
-	// Always check to make sure there is an active session
-	if ( m_lhSession )		
-	{
-		hRes = m_lpMapiResolveName (
-								     m_lhSession,	// Session handle
-									 0L,			// Parent window.
-									 const_cast<LPSTR>(lpszName),		// Name of recipient.  Passed in by argv.
-									 flFlags,		// Flags set to 0 for MAPIResolveName.
-									 ulReserved,
-									 &pRecips
-								  );				
 
-		if ( hRes == SUCCESS_SUCCESS )
-		{  
-			// Copy the recipient descriptor returned from MAPIResolveName to 
-			// the out parameter for this function,
-			*ppRecip = pRecips;
-		}  
-	}
+	const HRESULT hRes = m_lpMapiResolveName (
+							     m_lhSession,	// Session handle
+								 0L,			// Parent window.
+								 const_cast<LPSTR>(lpszName),		// Name of recipient.  Passed in by argv.
+								 flFlags,		// Flags set to 0 for MAPIResolveName.
+								 ulReserved,
+								 &pRecips
+								 );				
+
+	if ( hRes == SUCCESS_SUCCESS )
+	{  
+		// Copy the recipient descriptor returned from MAPIResolveName to 
+		// the out parameter for this function,
+		*ppRecip = pRecips;
+	}  
 	return hRes;
 }
 
-
-
 int CMailMsg::MAPISend()
 {
+	if (!initIfNeeded())
+	{
+		return 1;
+	}
 
    TStrStrVector::iterator p;
    int                  nIndex = 0;
-   size_t               nRecipients = 0;
-   MapiRecipDesc*       pRecipients = NULL;
    MapiRecipDesc*       pOriginator = NULL;
    MapiRecipDesc*       pFirstRecipient = NULL;
-   ULONG                nAttachments = 0;
-   MapiFileDesc*        pAttachments = NULL;
-   ULONG                status = 0;
    MapiMessage          message;
    std::vector<MapiRecipDesc*>	buffersToFree;
    MapiRecipDesc*       pRecip;
    MapiRecipDesc		grecip;
 
-   if (m_bReady || Initialize())
-   {
-	  LHANDLE hMapiSession;
-	  status = m_lpMapiLogon(NULL, NULL, NULL, MAPI_NEW_SESSION | MAPI_LOGON_UI, 0, &hMapiSession);
-	  if (SUCCESS_SUCCESS != status) {
-		  return FALSE;
-	  }
 
-      nRecipients = m_to.size() + m_cc.size() + m_bcc.size() + m_from.size();
-      if (nRecipients)
-	  {
-         pRecipients = new MapiRecipDesc[nRecipients];
-		 memset(pRecipients, 0, nRecipients * sizeof  MapiRecipDesc);
-	  }
+	LHANDLE hMapiSession;
+	ULONG status = m_lpMapiLogon(NULL, NULL, NULL, MAPI_NEW_SESSION | MAPI_LOGON_UI, 0, &hMapiSession);
+	if (SUCCESS_SUCCESS != status) {
+		return FALSE;
+	}
 
-      nAttachments = (ULONG)m_attachments.size();
-      if (nAttachments)
-         pAttachments = new MapiFileDesc[nAttachments];
+	const size_t nRecipients = m_to.size() + m_cc.size() + m_bcc.size() + m_from.size();
+	auto_buffer<MapiRecipDesc> pRecipients(nRecipients);
+	if (nRecipients)
+	{
+		memset(pRecipients, 0, nRecipients * sizeof  MapiRecipDesc);
+	}
 
-      if (pRecipients)
-      {
-         pFirstRecipient = pRecipients;
-         if (m_from.size())
-         {
-            // set from
-			 if (cResolveName(hMapiSession, m_from.begin()->first.c_str(), &pOriginator) == SUCCESS_SUCCESS) {
+	const ULONG nAttachments = (ULONG)m_attachments.size();
+	auto_buffer<MapiFileDesc> pAttachments(nAttachments);
+
+	if (pRecipients)
+	{
+		pFirstRecipient = pRecipients;
+		if (m_from.size())
+		{
+			// set from
+			if (cResolveName(hMapiSession, m_from.begin()->first.c_str(), &pOriginator) == SUCCESS_SUCCESS) {
 				buffersToFree.push_back(pOriginator);
-			 }
-         }
-         if (m_to.size())
-         {
-			 if (cResolveName(hMapiSession, m_to.begin()->first.c_str(), &pRecip) == SUCCESS_SUCCESS) {
+			}
+		}
+		if (m_to.size())
+		{
+			if (cResolveName(hMapiSession, m_to.begin()->first.c_str(), &pRecip) == SUCCESS_SUCCESS) {
 				if (pFirstRecipient == NULL)
 					pFirstRecipient = &pRecipients[nIndex];
 				pRecip->ulRecipClass = MAPI_TO;
 				memcpy(&pRecipients[nIndex], pRecip, sizeof pRecipients[nIndex]);
 				buffersToFree.push_back(pRecip);
-				nIndex++;
-			 }
-			 else
-			 {
-				 if (pFirstRecipient == NULL)
-					 pFirstRecipient = &pRecipients[nIndex];
-				 grecip.ulRecipClass = MAPI_TO;
-				 grecip.lpEntryID = 0;
-				 grecip.lpszName = 0;
-				 grecip.ulEIDSize = 0;
-				 grecip.ulReserved = 0;
-				 grecip.lpszAddress = (LPTSTR)(LPCTSTR)m_to.begin()->first.c_str();
-				 memcpy(&pRecipients[nIndex], &grecip, sizeof pRecipients[nIndex]);
-				 nIndex++;
-			 }
-         }		
-         if (m_cc.size())
-         {
-            // set cc's
-            for (p = m_cc.begin(); p != m_cc.end(); p++, nIndex++)
-            {
+			}
+			else
+			{
+				if (pFirstRecipient == NULL)
+					pFirstRecipient = &pRecipients[nIndex];
+				grecip.ulRecipClass = MAPI_TO;
+				grecip.lpEntryID = 0;
+				grecip.lpszName = 0;
+				grecip.ulEIDSize = 0;
+				grecip.ulReserved = 0;
+				grecip.lpszAddress = (LPTSTR)(LPCTSTR)m_to.begin()->first.c_str();
+				memcpy(&pRecipients[nIndex], &grecip, sizeof pRecipients[nIndex]);
+			}
+			nIndex++;
+		}		
+		if (m_cc.size())
+		{
+			// set cc's
+			for (p = m_cc.begin(); p != m_cc.end(); p++, nIndex++)
+			{
 				if ( cResolveName(hMapiSession, p->first.c_str(), &pRecip) == SUCCESS_SUCCESS) {
 					if (pFirstRecipient == NULL)
 						pFirstRecipient = &pRecipients[nIndex];
@@ -233,12 +229,12 @@
 					nIndex++;
 				}
             }
-         }
-   
-         if (m_bcc.size())
-         {
-            // set bcc
-            for (p = m_bcc.begin(); p != m_bcc.end(); p++, nIndex++)
+		}
+
+		if (m_bcc.size())
+		{
+			// set bcc
+			for (p = m_bcc.begin(); p != m_bcc.end(); p++, nIndex++)
             {
 				if ( cResolveName(hMapiSession, p->first.c_str(), &pRecip) == SUCCESS_SUCCESS) {
 					if (pFirstRecipient == NULL)
@@ -249,235 +245,228 @@
 					nIndex++;
 				}
             }
-         }
-      }
-      if (pAttachments)
-      {
-         // add attachments
-         for (p = m_attachments.begin(), nIndex = 0;
-              p != m_attachments.end(); p++, nIndex++)
-         {
-            pAttachments[nIndex].ulReserved        = 0;
+		}
+	}
+	if (pAttachments)
+	{
+		// add attachments
+		for (p = m_attachments.begin(), nIndex = 0;
+			p != m_attachments.end(); p++, nIndex++)
+		{
+			pAttachments[nIndex].ulReserved        = 0;
             pAttachments[nIndex].flFlags           = 0;
             pAttachments[nIndex].nPosition         = 0;
             pAttachments[nIndex].lpszPathName      = (LPTSTR)p->first.c_str();
             pAttachments[nIndex].lpszFileName      = (LPTSTR)p->second.c_str();
             pAttachments[nIndex].lpFileType        = NULL;
-         }
-      }
-	  memset(&message, 0, sizeof message);
-      message.ulReserved                        = 0;
-	  if (!m_sSubject.empty())
-	      message.lpszSubject                       = (LPTSTR)m_sSubject.c_str();
-	  else
-		  message.lpszSubject = "No Subject";
-	  if (!m_sMessage.empty())
-	      message.lpszNoteText                      = (LPTSTR)m_sMessage.c_str();
-	  else
-		  message.lpszNoteText = "No Message Body";
-      message.lpszMessageType                   = NULL;
-      message.lpszDateReceived                  = NULL;
-      message.lpszConversationID                = NULL;
-      message.flFlags                           = 0;
-      message.lpOriginator                      = pOriginator;
-      message.nRecipCount                       = nIndex;
-      message.lpRecips                          = pFirstRecipient;
-      message.nFileCount                        = nAttachments;
-      message.lpFiles                           = pAttachments;
+		}
+	}
+	memset(&message, 0, sizeof message);
+	message.ulReserved = 0;
+	if (m_sSubject.empty())
+		message.lpszSubject = "No Subject";
+	else
+		message.lpszSubject = (LPTSTR)m_sSubject.c_str();		
+	if (m_sMessage.empty())
+		message.lpszNoteText = "No Message Body";
+	else
+		message.lpszNoteText = (LPTSTR)m_sMessage.c_str();
 
-      status = m_lpMapiSendMail(hMapiSession, 0, &message, MAPI_DIALOG, 0);
-		  
-      m_lpMapiLogoff(hMapiSession, NULL, 0, 0);
-	  std::vector<MapiRecipDesc*>::iterator iter;
-	  for (iter = buffersToFree.begin(); iter != buffersToFree.end(); iter++) {
-		  m_lpMapiFreeBuffer(*iter);
-	  }
-if (SUCCESS_SUCCESS != status) {
-				string txt;
-				TCHAR buf[MAX_PATH];
-				_tprintf_s(buf, "Message did not get sent due to error code %d.\r\n", status);
-				txt = buf;
-			switch (status)
-			{  
-			case MAPI_E_AMBIGUOUS_RECIPIENT:
-				txt += "A recipient matched more than one of the recipient descriptor structures and MAPI_DIALOG was not set. No message was sent.\r\n" ;
-				break;
-			case MAPI_E_ATTACHMENT_NOT_FOUND:
-				txt += "The specified attachment was not found. No message was sent.\r\n" ;
-				break;
-			case MAPI_E_ATTACHMENT_OPEN_FAILURE:
-				txt += "The specified attachment could not be opened. No message was sent.\r\n" ;
-				break;
-			case MAPI_E_BAD_RECIPTYPE:
-				txt += "The type of a recipient was not MAPI_TO, MAPI_CC, or MAPI_BCC. No message was sent.\r\n" ;
-				break;
-			case MAPI_E_FAILURE:
-				txt += "One or more unspecified errors occurred. No message was sent.\r\n" ;
-				break;
-			case MAPI_E_INSUFFICIENT_MEMORY:
-				txt += "There was insufficient memory to proceed. No message was sent.\r\n" ;
-				break;
-			case MAPI_E_INVALID_RECIPS:
-				txt += "One or more recipients were invalid or did not resolve to any address.\r\n" ;
-				break;
-			case MAPI_E_LOGIN_FAILURE:
-				txt += "There was no default logon, and the user failed to log on successfully when the logon dialog box was displayed. No message was sent.\r\n" ;
-				break;
-			case MAPI_E_TEXT_TOO_LARGE:
-				txt += "The text in the message was too large. No message was sent.\r\n" ;
-				break;
-			case MAPI_E_TOO_MANY_FILES:
-				txt += "There were too many file attachments. No message was sent.\r\n" ;
-				break;
-			case MAPI_E_TOO_MANY_RECIPIENTS:
-				txt += "There were too many recipients. No message was sent.\r\n" ;
-				break;
-			case MAPI_E_UNKNOWN_RECIPIENT:
-				txt += "A recipient did not appear in the address list. No message was sent.\r\n" ;
-				break;
-			case MAPI_E_USER_ABORT:
-				txt += "The user canceled one of the dialog boxes. No message was sent.\r\n" ;
-				break;
-			default:
-				txt += "Unknown error code.\r\n" ;
-				break;
-			}
-			::MessageBox(0, txt.c_str(), "Error", MB_OK);
-}
+	message.lpszMessageType                   = NULL;
+	message.lpszDateReceived                  = NULL;
+	message.lpszConversationID                = NULL;
+	message.flFlags                           = 0;
+	message.lpOriginator                      = pOriginator;
+	message.nRecipCount                       = nIndex;
+	message.lpRecips                          = pFirstRecipient;
+	message.nFileCount                        = nAttachments;
+	message.lpFiles                           = pAttachments;
 
-      if (pRecipients)
-         delete [] pRecipients;
+	status = m_lpMapiSendMail(hMapiSession, 0, &message, MAPI_DIALOG, 0);
+	m_lpMapiLogoff(hMapiSession, NULL, 0, 0);
 
-      if (nAttachments)
-         delete [] pAttachments;
-   }
+	std::vector<MapiRecipDesc*>::iterator iter;
+	for (iter = buffersToFree.begin(); iter != buffersToFree.end(); iter++) {
+		m_lpMapiFreeBuffer(*iter);
+	}
 
-   if (SUCCESS_SUCCESS == status)
-	   return 1;
-   if (MAPI_E_USER_ABORT == status)
-	   return -1;
-   // other failure
-   return 0;
+	if (SUCCESS_SUCCESS == status) {
+		return 1;
+	}
+
+	string txt;
+	TCHAR buf[MAX_PATH];
+	_tprintf_s(buf, "Message did not get sent due to error code %d.\r\n", status);
+	txt = buf;
+	switch (status)
+	{  
+	case MAPI_E_AMBIGUOUS_RECIPIENT:
+		txt += "A recipient matched more than one of the recipient descriptor structures and MAPI_DIALOG was not set. No message was sent.\r\n" ;
+		break;
+	case MAPI_E_ATTACHMENT_NOT_FOUND:
+		txt += "The specified attachment was not found. No message was sent.\r\n" ;
+		break;
+	case MAPI_E_ATTACHMENT_OPEN_FAILURE:
+		txt += "The specified attachment could not be opened. No message was sent.\r\n" ;
+		break;
+	case MAPI_E_BAD_RECIPTYPE:
+		txt += "The type of a recipient was not MAPI_TO, MAPI_CC, or MAPI_BCC. No message was sent.\r\n" ;
+		break;
+	case MAPI_E_FAILURE:
+		txt += "One or more unspecified errors occurred. No message was sent.\r\n" ;
+		break;
+	case MAPI_E_INSUFFICIENT_MEMORY:
+		txt += "There was insufficient memory to proceed. No message was sent.\r\n" ;
+		break;
+	case MAPI_E_INVALID_RECIPS:
+		txt += "One or more recipients were invalid or did not resolve to any address.\r\n" ;
+		break;
+	case MAPI_E_LOGIN_FAILURE:
+		txt += "There was no default logon, and the user failed to log on successfully when the logon dialog box was displayed. No message was sent.\r\n" ;
+		break;
+	case MAPI_E_TEXT_TOO_LARGE:
+		txt += "The text in the message was too large. No message was sent.\r\n" ;
+		break;
+	case MAPI_E_TOO_MANY_FILES:
+		txt += "There were too many file attachments. No message was sent.\r\n" ;
+		break;
+	case MAPI_E_TOO_MANY_RECIPIENTS:
+		txt += "There were too many recipients. No message was sent.\r\n" ;
+		break;
+	case MAPI_E_UNKNOWN_RECIPIENT:
+		txt += "A recipient did not appear in the address list. No message was sent.\r\n" ;
+		break;
+	case MAPI_E_USER_ABORT:
+		txt += "The user canceled one of the dialog boxes. No message was sent.\r\n" ;
+		break;
+	default:
+		txt += "Unknown error code.\r\n" ;
+		break;
+	}
+	::MessageBox(0, txt.c_str(), "Error", MB_OK);
+
+	pRecipients.reset();
+	pAttachments.reset();
+
+	if (MAPI_E_USER_ABORT == status)
+		return -1;
+	// other failure
+	return 0;
 }
 
 BOOL CMailMsg::CMCSend()
 {
-   TStrStrVector::iterator p;
-   int                  nIndex = 0;
-   CMC_recipient*       pRecipients;
-   CMC_attachment*      pAttachments;
-   CMC_session_id       session;
-   CMC_return_code      status = 0;
-   CMC_message          message;
-   CMC_boolean          bAvailable = FALSE;
-   CMC_time             t_now = {0};
+	if (!initIfNeeded())
+	{
+		return FALSE;
+	}
 
-   if (m_bReady || Initialize())
-   {
-      pRecipients = new CMC_recipient[m_to.size() + m_cc.size() + m_bcc.size() + m_from.size()];
-      pAttachments = new CMC_attachment[m_attachments.size()];
+	auto_buffer<CMC_recipient> pRecipients(m_to.size() + m_cc.size() + m_bcc.size() + m_from.size());
+	auto_buffer<CMC_attachment> pAttachments(m_attachments.size());
 
-      // set cc's
-      for (p = m_cc.begin(); p != m_cc.end(); p++, nIndex++)
-      {
-         pRecipients[nIndex].name                = (LPTSTR)(LPCTSTR)p->second.c_str();
-         pRecipients[nIndex].name_type           = CMC_TYPE_INDIVIDUAL;
-         pRecipients[nIndex].address             = (LPTSTR)(LPCTSTR)p->first.c_str();
-         pRecipients[nIndex].role                = CMC_ROLE_CC;
-         pRecipients[nIndex].recip_flags         = 0;
-         pRecipients[nIndex].recip_extensions    = NULL;
-      }
-   
-      // set bcc
-      for (p = m_bcc.begin(); p != m_bcc.end(); p++, nIndex++)
-      {
-         pRecipients[nIndex].name                = (LPTSTR)(LPCTSTR)p->second.c_str();
-         pRecipients[nIndex].name_type           = CMC_TYPE_INDIVIDUAL;
-         pRecipients[nIndex].address             = (LPTSTR)(LPCTSTR)p->first.c_str();
-         pRecipients[nIndex].role                = CMC_ROLE_BCC;
-         pRecipients[nIndex].recip_flags         = 0;
-         pRecipients[nIndex].recip_extensions    = NULL;
-      }
-   
-      // set to
-      pRecipients[nIndex].name                   = (LPTSTR)(LPCTSTR)m_to.begin()->second.c_str();
-      pRecipients[nIndex].name_type              = CMC_TYPE_INDIVIDUAL;
-      pRecipients[nIndex].address                = (LPTSTR)(LPCTSTR)m_to.begin()->first.c_str();
-      pRecipients[nIndex].role                   = CMC_ROLE_TO;
-      pRecipients[nIndex].recip_flags            = 0;
-      pRecipients[nIndex].recip_extensions       = NULL;
-   
-      // set from
-      pRecipients[nIndex+1].name                 = (LPTSTR)(LPCTSTR)m_from.begin()->second.c_str();
-      pRecipients[nIndex+1].name_type            = CMC_TYPE_INDIVIDUAL;
-      pRecipients[nIndex+1].address              = (LPTSTR)(LPCTSTR)m_from.begin()->first.c_str();
-      pRecipients[nIndex+1].role                 = CMC_ROLE_ORIGINATOR;
-      pRecipients[nIndex+1].recip_flags          = CMC_RECIP_LAST_ELEMENT;
-      pRecipients[nIndex+1].recip_extensions     = NULL;
-   
-      // add attachments
-      for (p = m_attachments.begin(), nIndex = 0;
-           p != m_attachments.end(); p++, nIndex++)
-      {
-         pAttachments[nIndex].attach_title       = (LPTSTR)(LPCTSTR)p->second.c_str();
-         pAttachments[nIndex].attach_type        = NULL;
-         pAttachments[nIndex].attach_filename    = (LPTSTR)(LPCTSTR)p->first.c_str();
-         pAttachments[nIndex].attach_flags       = 0;
-         pAttachments[nIndex].attach_extensions  = NULL;
-      }
-      pAttachments[nIndex-1].attach_flags        = CMC_ATT_LAST_ELEMENT;
+	TStrStrVector::iterator p;
+	int                  nIndex = 0;
+	CMC_message          message;
+	CMC_boolean          bAvailable = FALSE;
+	CMC_time             t_now = {0};
+	
+	// set cc's
+	for (p = m_cc.begin(); p != m_cc.end(); p++, nIndex++)
+	{
+		pRecipients[nIndex].name                = (LPTSTR)(LPCTSTR)p->second.c_str();
+		pRecipients[nIndex].name_type           = CMC_TYPE_INDIVIDUAL;
+		pRecipients[nIndex].address             = (LPTSTR)(LPCTSTR)p->first.c_str();
+		pRecipients[nIndex].role                = CMC_ROLE_CC;
+		pRecipients[nIndex].recip_flags         = 0;
+		pRecipients[nIndex].recip_extensions    = NULL;
+	}
 
-      message.message_reference                 = NULL;
-      message.message_type                      = NULL;
-	  if (m_sSubject.empty())
-		  message.subject = "No Subject";
-	  else
-	      message.subject                           = (LPTSTR)(LPCTSTR)m_sSubject.c_str();
-      message.time_sent                         = t_now;
-	  if (m_sMessage.empty())
-		  message.text_note = "No Body";
-	  else
-		  message.text_note                         = (LPTSTR)(LPCTSTR)m_sMessage.c_str();
-      message.recipients                        = pRecipients;
-      message.attachments                       = pAttachments;
-      message.message_flags                     = 0;
-      message.message_extensions                = NULL;
+	// set bcc
+	for (p = m_bcc.begin(); p != m_bcc.end(); p++, nIndex++)
+	{
+		pRecipients[nIndex].name                = (LPTSTR)(LPCTSTR)p->second.c_str();
+		pRecipients[nIndex].name_type           = CMC_TYPE_INDIVIDUAL;
+		pRecipients[nIndex].address             = (LPTSTR)(LPCTSTR)p->first.c_str();
+		pRecipients[nIndex].role                = CMC_ROLE_BCC;
+		pRecipients[nIndex].recip_flags         = 0;
+		pRecipients[nIndex].recip_extensions    = NULL;
+	}
 
-      status = m_lpCmcQueryConfiguration(
-                  0, 
-                  CMC_CONFIG_UI_AVAIL, 
-                  (void*)&bAvailable, 
-                  NULL
-                  );
+	// set to
+	pRecipients[nIndex].name                   = (LPTSTR)(LPCTSTR)m_to.begin()->second.c_str();
+	pRecipients[nIndex].name_type              = CMC_TYPE_INDIVIDUAL;
+	pRecipients[nIndex].address                = (LPTSTR)(LPCTSTR)m_to.begin()->first.c_str();
+	pRecipients[nIndex].role                   = CMC_ROLE_TO;
+	pRecipients[nIndex].recip_flags            = 0;
+	pRecipients[nIndex].recip_extensions       = NULL;
 
-      if (CMC_SUCCESS == status && bAvailable)
-      {
-         status = m_lpCmcLogon(
-                     NULL,
-                     NULL,
-                     NULL,
-                     NULL,
-                     0,
-                     CMC_VERSION,
-                     CMC_LOGON_UI_ALLOWED |
-                     CMC_ERROR_UI_ALLOWED,
-                     &session,
-                     NULL
-                     );
+	// set from
+	pRecipients[nIndex+1].name                 = (LPTSTR)(LPCTSTR)m_from.begin()->second.c_str();
+	pRecipients[nIndex+1].name_type            = CMC_TYPE_INDIVIDUAL;
+	pRecipients[nIndex+1].address              = (LPTSTR)(LPCTSTR)m_from.begin()->first.c_str();
+	pRecipients[nIndex+1].role                 = CMC_ROLE_ORIGINATOR;
+	pRecipients[nIndex+1].recip_flags          = CMC_RECIP_LAST_ELEMENT;
+	pRecipients[nIndex+1].recip_extensions     = NULL;
 
-         if (CMC_SUCCESS == status)
-         {
-            status = m_lpCmcSend(session, &message, 0, 0, NULL);
+	// add attachments
+	for (p = m_attachments.begin(), nIndex = 0;
+		p != m_attachments.end(); p++, nIndex++)
+	{
+		pAttachments[nIndex].attach_title       = (LPTSTR)(LPCTSTR)p->second.c_str();
+		pAttachments[nIndex].attach_type        = NULL;
+		pAttachments[nIndex].attach_filename    = (LPTSTR)(LPCTSTR)p->first.c_str();
+		pAttachments[nIndex].attach_flags       = 0;
+		pAttachments[nIndex].attach_extensions  = NULL;
+	}
+	pAttachments[nIndex-1].attach_flags = CMC_ATT_LAST_ELEMENT;
 
-            m_lpCmcLogoff(session, NULL, CMC_LOGON_UI_ALLOWED, NULL);
-         }
-      }
+	message.message_reference = NULL;
+	message.message_type = NULL;
+	if (m_sSubject.empty())
+		message.subject = "No Subject";
+	else
+		message.subject = (LPTSTR)(LPCTSTR)m_sSubject.c_str();
+	message.time_sent = t_now;
+	if (m_sMessage.empty())
+		message.text_note = "No Body";
+	else
+		message.text_note = (LPTSTR)(LPCTSTR)m_sMessage.c_str();
 
-      delete [] pRecipients;
-      delete [] pAttachments;
-   }
+	message.recipients                        = pRecipients;
+	message.attachments                       = pAttachments;
+	message.message_flags                     = 0;
+	message.message_extensions                = NULL;
 
-   return ((CMC_SUCCESS == status) && bAvailable);
+	CMC_return_code status = m_lpCmcQueryConfiguration(
+								0,
+								CMC_CONFIG_UI_AVAIL,
+								(void*)&bAvailable,
+								NULL
+								);
+
+	if (CMC_SUCCESS == status && bAvailable)
+	{
+		CMC_session_id session;
+		status = m_lpCmcLogon(
+					NULL,
+					NULL,
+					NULL,
+					NULL,
+					0,
+					CMC_VERSION,
+					CMC_LOGON_UI_ALLOWED |
+					CMC_ERROR_UI_ALLOWED,
+					&session,
+					NULL
+					);
+
+		if (CMC_SUCCESS == status)
+		{
+			status = m_lpCmcSend(session, &message, 0, 0, NULL);
+			m_lpCmcLogoff(session, NULL, CMC_LOGON_UI_ALLOWED, NULL);
+		}
+	}
+	return ((CMC_SUCCESS == status) && bAvailable);
 }
 
 BOOL CMailMsg::Initialize()
@@ -506,4 +495,9 @@
 void CMailMsg::Uninitialize()
 {
    ::FreeLibrary(m_hMapi);
-}
\ No newline at end of file
+}
+
+bool CMailMsg::initIfNeeded()
+{
+	return (m_bReady || Initialize());
+}
Index: crashrpt/MailMsg.h
===================================================================
--- crashrpt/MailMsg.h	(revision 17027)
+++ crashrpt/MailMsg.h	(working copy)
@@ -317,4 +317,7 @@
    LPMAPIFREEBUFFER m_lpMapiFreeBuffer;         // Mapi func pointer
    
    BOOL           m_bReady;                     // MAPI is loaded
+
+private:
+	bool initIfNeeded();
 };
Index: crashrpt/MainDlg.cpp
===================================================================
--- crashrpt/MainDlg.cpp	(revision 17027)
+++ crashrpt/MainDlg.cpp	(working copy)
@@ -2,12 +2,13 @@
 
 #include "MainDlg.h"
 #include "resource.h"
+#include "auto_buffer.h"
 
-CMainDlg::CMainDlg(void)
+CMainDlg::CMainDlg()
 {
 }
 
-CMainDlg::~CMainDlg(void)
+CMainDlg::~CMainDlg()
 {
 }
 
@@ -80,15 +81,13 @@
 			int      nEmailLen = ::GetWindowTextLength(hWndEmail) + 1;
 			int      nDescLen = ::GetWindowTextLength(hWndDesc) + 1;
 
-			TCHAR * lpStr = new TCHAR[nEmailLen+1];
+			auto_buffer<TCHAR> lpStr(nEmailLen+1);
 			::GetWindowText(hWndEmail, lpStr, nEmailLen);
 			m_sEmail = lpStr;
-			delete [] lpStr;
 
-			lpStr = new TCHAR[nDescLen+1];
+			lpStr.reset(nDescLen+1);
 			::GetWindowText(hWndDesc, lpStr, nDescLen);
 			m_sDescription = lpStr;
-			delete [] lpStr;
 		}
 		EndDialog(*this, IDOK);
 		return 0;
Index: crashrpt/SymbolEngine.h
===================================================================
--- crashrpt/SymbolEngine.h	(revision 17027)
+++ crashrpt/SymbolEngine.h	(working copy)
@@ -30,6 +30,7 @@
 // You could include either IMAGEHLP.DLL or DBGHELP.DLL.
 #include "imagehlp.h"
 #include <tchar.h>
+#include "auto_buffer.h"
 
 // Include these in case the user forgets to link against them.
 #pragma comment (lib,"dbghelp.lib")
@@ -156,14 +157,13 @@
         }
 
         // Got the version size, now get the version information.
-        LPVOID lpData = (LPVOID)new TCHAR [ dwVerSize ] ;
+        auto_buffer<TCHAR> lpData(dwVerSize);
         if ( FALSE == GetFileVersionInfo ( szImageHlp       ,
                                            dwVerInfoHandle  ,
                                            dwVerSize        ,
-                                           lpData            ) )
+                                           lpData ) )
         {
-            delete [] lpData ;
-            return ( FALSE ) ;
+            return FALSE;
         }
 
         VS_FIXEDFILEINFO * lpVerInfo ;
@@ -178,9 +178,7 @@
             dwLS = lpVerInfo->dwFileVersionLS ;
         }
 
-        delete [] lpData ;
-
-        return ( bRet ) ;
+		return bRet;
     }
 
 /*----------------------------------------------------------------------
Index: TortoiseBlame/TortoiseBlame.cpp
===================================================================
--- TortoiseBlame/TortoiseBlame.cpp	(revision 17027)
+++ TortoiseBlame/TortoiseBlame.cpp	(working copy)
@@ -21,6 +21,7 @@
 #include "TortoiseBlame.h"
 #include "registry.h"
 #include "LangDll.h"
+#include "auto_buffer.h"
 
 #define MAX_LOADSTRING 1000
 
@@ -98,10 +99,9 @@
 	do 
 	{
 		bufferlen += MAX_PATH;		// MAX_PATH is not the limit here!
-		TCHAR * pBuf = new TCHAR[bufferlen];
+		auto_buffer<TCHAR> pBuf(bufferlen);
 		len = GetModuleFileName(NULL, pBuf, bufferlen);	
 		path = std::string(pBuf, len);
-		delete [] pBuf;
 	} while(len == bufferlen);
 	path = path.substr(0, path.rfind('\\') + 1);
 
@@ -502,13 +502,7 @@
 
 	if(!bCaseSensitive)
 	{
-		char *p;
-		size_t len = strlen(szWhat);
-		for (p = szWhat; p < szWhat + len; p++)
-		{
-			if (isupper(*p)&&__isascii(*p))
-				*p = _tolower(*p);
-		}
+		makeLower(szWhat, strlen(szWhat));
 	}
 
 	std::string sWhat = std::string(szWhat);
@@ -517,18 +511,13 @@
 	int i=0;
 	for (i=line; (i<(int)authors.size())&&(!bFound); ++i)
 	{
-		int bufsize = (int)SendEditor(SCI_GETLINE, i);
-		char * linebuf = new char[bufsize+1];
+		const int bufsize = (int)SendEditor(SCI_GETLINE, i);
+		auto_buffer<char> linebuf(bufsize+1);
 		SecureZeroMemory(linebuf, bufsize+1);
 		SendEditor(SCI_GETLINE, i, (LPARAM)linebuf);
 		if (!bCaseSensitive)
 		{
-			char *p;
-			for (p = linebuf; p < linebuf + bufsize; p++)
-			{
-				if (isupper(*p)&&__isascii(*p))
-					*p = _tolower(*p);
-			}
+			makeLower(lineBuf, bufsize);
 		}
 		_stprintf_s(buf, 20, _T("%ld"), revs[i]);
 		if (authors[i].compare(sWhat)==0)
@@ -539,24 +528,18 @@
 			bFound = true;
 		else if (strstr(linebuf, szWhat))
 			bFound = true;
-		delete [] linebuf;
 	}
 	if (!bFound)
 	{
 		for (i=0; (i<line)&&(!bFound); ++i)
 		{
-			int bufsize = (int)SendEditor(SCI_GETLINE, i);
-			char * linebuf = new char[bufsize+1];
+			const int bufsize = (int)SendEditor(SCI_GETLINE, i);
+			auto_buffer<char> linebuf(bufsize+1);
 			SecureZeroMemory(linebuf, bufsize+1);
 			SendEditor(SCI_GETLINE, i, (LPARAM)linebuf);
 			if (!bCaseSensitive)
 			{
-				char *p;
-				for (p = linebuf; p < linebuf + bufsize; p++)
-				{
-					if (isupper(*p)&&__isascii(*p))
-						*p = _tolower(*p);
-				}
+				makeLower(linebuf, bufsize);
 			}
 			_stprintf_s(buf, 20, _T("%ld"), revs[i]);
 			if (authors[i].compare(sWhat)==0)
@@ -567,7 +550,6 @@
 				bFound = true;
 			else if (strstr(linebuf, szWhat))
 				bFound = true;
-			delete [] linebuf;
 		}
 	}
 	if (bFound)
@@ -1203,6 +1185,15 @@
 	} while (cPos != NULL);
 }
 
+void TortoiseBlame::makeLower( char* buffer, size_t len )
+{
+	for (char *p = buffer; p < szWhat + len; p++)
+	{
+		if (isupper(*p)&&__isascii(*p))
+			*p = _tolower(*p);
+	}
+}
+
 // Forward declarations of functions included in this code module:
 ATOM				MyRegisterClass(HINSTANCE hResource);
 ATOM				MyRegisterBlameClass(HINSTANCE hResource);
Index: TortoiseBlame/TortoiseBlame.h
===================================================================
--- TortoiseBlame/TortoiseBlame.h	(revision 17027)
+++ TortoiseBlame/TortoiseBlame.h	(working copy)
@@ -158,4 +158,7 @@
 
 	CRegStdDWORD					m_regOldLinesColor;
 	CRegStdDWORD					m_regNewLinesColor;
+
+private:
+	static void makeLower( char* buffer, size_t length );
 };
Index: TortoiseMerge/MainFrm.cpp
===================================================================
--- TortoiseMerge/MainFrm.cpp	(revision 17027)
+++ TortoiseMerge/MainFrm.cpp	(working copy)
@@ -31,6 +31,7 @@
 #include "BottomView.h"
 #include "DiffColors.h"
 #include ".\mainfrm.h"
+#include "auto_buffer.h"
 
 #ifdef _DEBUG
 #define new DEBUG_NEW
@@ -1063,7 +1064,7 @@
 		CString cmd = _T("\"") + CPathUtils::GetAppDirectory();
 		cmd += _T("TortoiseProc.exe\" /command:add /noui /path:\"");
 		cmd += m_Data.m_mergedFile.GetFilename() + _T("\"");
-		TCHAR * buf = new TCHAR[cmd.GetLength()+1];
+		auto_buffer<TCHAR> buf(cmd.GetLength()+1);
 		_tcscpy_s(buf, cmd.GetLength()+1, cmd);
 		STARTUPINFO startup;
 		PROCESS_INFORMATION process;
@@ -1072,7 +1073,6 @@
 		memset(&process, 0, sizeof(process));
 		if (CreateProcess(NULL, buf, NULL, NULL, FALSE, 0, 0, 0, &startup, &process)==0)
 		{
-			delete [] buf;
 			LPVOID lpMsgBuf;
 			FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
 				FORMAT_MESSAGE_FROM_SYSTEM | 
@@ -1088,7 +1088,6 @@
 			LocalFree( lpMsgBuf );
 			return FALSE;
 		}
-		delete [] buf;
 		CloseHandle(process.hThread);
 		CloseHandle(process.hProcess);
 	}
@@ -1130,7 +1129,7 @@
 	ofn.Flags = OFN_OVERWRITEPROMPT;
 	CString sFilter;
 	sFilter.LoadString(IDS_COMMONFILEFILTER);
-	TCHAR * pszFilters = new TCHAR[sFilter.GetLength()+4];
+	auto_buffer<TCHAR> pszFilters(sFilter.GetLength()+4);
 	_tcscpy_s (pszFilters, sFilter.GetLength()+4, sFilter);
 	// Replace '|' delimiters with '\0's
 	TCHAR *ptr = pszFilters + _tcslen(pszFilters);  //set ptr at the NULL
@@ -1149,10 +1148,8 @@
 	{
 		sFile = CString(ofn.lpstrFile);
 		SaveFile(sFile);
-		delete [] pszFilters;
 		return true;
 	}
-	delete [] pszFilters;
 	return false;
 }
 
@@ -2010,7 +2007,7 @@
 		ofn.Flags = OFN_OVERWRITEPROMPT;
 		CString sFilter;
 		sFilter.LoadString(IDS_COMMONFILEFILTER);
-		TCHAR * pszFilters = new TCHAR[sFilter.GetLength()+4];
+		auto_buffer<TCHAR> pszFilters(sFilter.GetLength()+4);
 		_tcscpy_s (pszFilters, sFilter.GetLength()+4, sFilter);
 		// Replace '|' delimiters with '\0's
 		TCHAR *ptr = pszFilters + _tcslen(pszFilters);  //set ptr at the NULL
@@ -2030,7 +2027,6 @@
 			outputFile = CString(ofn.lpstrFile);
 			CAppUtils::CreateUnifiedDiff(origFile, modifiedFile, outputFile, true);
 		}
-		delete [] pszFilters;
 	}
 }
 
Index: TortoiseMerge/OpenDlg.cpp
===================================================================
--- TortoiseMerge/OpenDlg.cpp	(revision 17027)
+++ TortoiseMerge/OpenDlg.cpp	(working copy)
@@ -20,8 +20,8 @@
 #include "TortoiseMerge.h"
 #include "BrowseFolder.h"
 #include ".\opendlg.h"
+#include "auto_buffer.h"
 
-
 // COpenDlg dialog
 
 IMPLEMENT_DYNAMIC(COpenDlg, CDialog)
@@ -155,7 +155,7 @@
 	ofn.nMaxFile = sizeof(szFile)/sizeof(TCHAR);
 	CString sFilter;
 	sFilter.LoadString(nFileFilter);
-	TCHAR * pszFilters = new TCHAR[sFilter.GetLength()+4];
+	auto_buffer<TCHAR> pszFilters(sFilter.GetLength()+4);
 	_tcscpy_s (pszFilters, sFilter.GetLength()+4, sFilter);
 	// Replace '|' delimiters with '\0's
 	TCHAR *ptr = pszFilters + _tcslen(pszFilters);  //set ptr at the NULL
@@ -178,10 +178,8 @@
 	if (GetOpenFileName(&ofn)==TRUE)
 	{
 		filepath = CString(ofn.lpstrFile);
-		delete [] pszFilters;
 		return TRUE;
 	}
-	delete [] pszFilters;
 	return FALSE;			//user canceled the dialog
 }
 
@@ -292,13 +290,11 @@
 			LPCSTR lpstr = (LPCSTR)GlobalLock(hglb); 
 
 			DWORD len = GetTempPath(0, NULL);
-			TCHAR * path = new TCHAR[len+1];
-			TCHAR * tempF = new TCHAR[len+100];
+			auto_buffer<TCHAR> path(len+1);
+			auto_buffer<TCHAR> tempF(len+100);
 			GetTempPath (len+1, path);
 			GetTempFileName (path, TEXT("tsm"), 0, tempF);
 			CString sTempFile = CString(tempF);
-			delete [] path;
-			delete [] tempF;
 
 			FILE * outFile;
 			size_t patchlen = strlen(lpstr);
Index: TortoiseMerge/TempFiles.cpp
===================================================================
--- TortoiseMerge/TempFiles.cpp	(revision 17027)
+++ TortoiseMerge/TempFiles.cpp	(working copy)
@@ -18,6 +18,7 @@
 //
 #include "StdAfx.h"
 #include ".\tempfiles.h"
+#include "auto_buffer.h"
 
 CTempFiles::CTempFiles(void)
 {
@@ -34,13 +35,11 @@
 CString CTempFiles::GetTempFilePath()
 {
 	DWORD len = GetTempPath(0, NULL);
-	TCHAR * path = new TCHAR[len+1];
-	TCHAR * tempF = new TCHAR[len+100];
+	auto_buffer<TCHAR> path(len+1);
+	auto_buffer<TCHAR> tempF(len+100);
 	GetTempPath (len+1, path);
 	GetTempFileName (path, TEXT("tsm"), 0, tempF);
 	CString tempfile = CString(tempF);
-	delete [] path;
-	delete [] tempF;
 	m_arTempFileList.Add(tempfile);
 	return tempfile;
 }
Index: TortoiseMerge/TortoiseMerge.cpp
===================================================================
--- TortoiseMerge/TortoiseMerge.cpp	(revision 17027)
+++ TortoiseMerge/TortoiseMerge.cpp	(working copy)
@@ -27,6 +27,7 @@
 #include "PathUtils.h"
 #include "BrowseFolder.h"
 #include "DirFileEnum.h"
+#include "auto_buffer.h"
 
 #ifdef _DEBUG
 #define new DEBUG_NEW
@@ -275,7 +276,7 @@
 
 		CString sFilter;
 		sFilter.LoadString(IDS_PATCHFILEFILTER);
-		TCHAR * pszFilters = new TCHAR[sFilter.GetLength()+4];
+		auto_buffer<TCHAR> pszFilters(sFilter.GetLength()+4);
 		_tcscpy_s (pszFilters, sFilter.GetLength()+4, sFilter);
 		// Replace '|' delimiters with '\0's
 		TCHAR *ptr = pszFilters + _tcslen(pszFilters);  //set ptr at the NULL
@@ -292,10 +293,8 @@
 		CString tempfile;
 		if (GetOpenFileName(&ofn)==FALSE)
 		{
-			delete [] pszFilters;
 			return FALSE;
 		}
-		delete [] pszFilters;
 		pFrame->m_Data.m_sDiffFile = ofn.lpstrFile;
 	}
 
@@ -400,7 +399,7 @@
 				ofn.Flags = OFN_OVERWRITEPROMPT;
 				CString sFilter;
 				sFilter.LoadString(IDS_COMMONFILEFILTER);
-				TCHAR * pszFilters = new TCHAR[sFilter.GetLength()+4];
+				auto_buffer<TCHAR> pszFilters(sFilter.GetLength()+4);
 				_tcscpy_s (pszFilters, sFilter.GetLength()+4, sFilter);
 				// Replace '|' delimiters with '\0's
 				TCHAR *ptr = pszFilters + _tcslen(pszFilters);  //set ptr at the NULL
@@ -419,7 +418,6 @@
 				{
 					outfile = CString(ofn.lpstrFile);
 				}
-				delete [] pszFilters;
 			}
 			if (!outfile.IsEmpty())
 			{
@@ -466,13 +464,11 @@
 			LPCSTR lpstr = (LPCSTR)GlobalLock(hglb); 
 
 			DWORD len = GetTempPath(0, NULL);
-			TCHAR * path = new TCHAR[len+1];
-			TCHAR * tempF = new TCHAR[len+100];
+			auto_buffer<TCHAR> path(len+1);
+			auto_buffer<TCHAR> tempF(len+100);
 			GetTempPath (len+1, path);
 			GetTempFileName (path, TEXT("tsm"), 0, tempF);
 			std::wstring sTempFile = std::wstring(tempF);
-			delete [] path;
-			delete [] tempF;
 
 			FILE * outFile;
 			size_t patchlen = strlen(lpstr);
@@ -499,7 +495,7 @@
 	// Look for temporary files left around by TortoiseMerge and
 	// remove them. But only delete 'old' files 
 	DWORD len = ::GetTempPath(0, NULL);
-	TCHAR * path = new TCHAR[len + 100];
+	auto_buffer<TCHAR> path(len + 100);
 	len = ::GetTempPath (len+100, path);
 	if (len != 0)
 	{
@@ -529,7 +525,6 @@
 			}
 		}
 	}	
-	delete[] path;		
 
 	return CWinAppEx::ExitInstance();
 }
Index: TortoiseProc/AppUtils.cpp
===================================================================
--- TortoiseProc/AppUtils.cpp	(revision 17027)
+++ TortoiseProc/AppUtils.cpp	(working copy)
@@ -30,8 +30,8 @@
 #include "RepositoryBrowser.h"
 #include "BrowseFolder.h"
 #include <intshcut.h>
+#include "auto_buffer.h"
 
-
 CAppUtils::CAppUtils(void)
 {
 }
@@ -429,15 +429,13 @@
 	viewer = txtexe;
 
 	DWORD len = ExpandEnvironmentStrings(viewer, NULL, 0);
-	TCHAR * buf = new TCHAR[len+1];
+	auto_buffer<TCHAR> buf(len+1);
 	ExpandEnvironmentStrings(viewer, buf, len);
 	viewer = buf;
-	delete [] buf;
 	len = ExpandEnvironmentStrings(file, NULL, 0);
-	buf = new TCHAR[len+1];
+	buf.reset(len+1);
 	ExpandEnvironmentStrings(file, buf, len);
 	file = buf;
-	delete [] buf;
 	file = _T("\"")+file+_T("\"");
 	if (viewer.IsEmpty())
 	{
@@ -450,8 +448,9 @@
 		ofn.nMaxFile = sizeof(szFile)/sizeof(TCHAR);
 		CString sFilter;
 		sFilter.LoadString(IDS_PROGRAMSFILEFILTER);
-		TCHAR * pszFilters = new TCHAR[sFilter.GetLength()+4];
-		_tcscpy_s (pszFilters, sFilter.GetLength()+4, sFilter);
+		const int filterLength = sFilter.GetLength()+4;
+		auto_buffer<TCHAR> pszFilters(filterLength);
+		_tcscpy_s (pszFilters, filterLength, sFilter);
 		// Replace '|' delimiters with '\0's
 		TCHAR *ptr = pszFilters + _tcslen(pszFilters);  //set ptr at the NULL
 		while (ptr != pszFilters)
@@ -475,12 +474,10 @@
 
 		if (GetOpenFileName(&ofn)==TRUE)
 		{
-			delete [] pszFilters;
 			viewer = CString(ofn.lpstrFile);
 		}
 		else
 		{
-			delete [] pszFilters;
 			return FALSE;
 		}
 	}
@@ -868,12 +865,13 @@
 	ofn.lpstrFile = szFile;
 	ofn.nMaxFile = sizeof(szFile)/sizeof(TCHAR);
 	CString sFilter;
-	TCHAR * pszFilters = NULL;
+	auto_buffer<TCHAR> pszFilters;
 	if (filter)
 	{
 		sFilter.LoadString(filter);
-		pszFilters = new TCHAR[sFilter.GetLength()+4];
-		_tcscpy_s (pszFilters, sFilter.GetLength()+4, sFilter);
+		const int filtersLength = sFilter.GetLength()+4;
+		pszFilters.reset(filtersLength);
+		_tcscpy_s (pszFilters, filtersLength, sFilter);
 		// Replace '|' delimiters with '\0's
 		TCHAR *ptr = pszFilters + _tcslen(pszFilters);  //set ptr at the NULL
 		while (ptr != pszFilters)
@@ -913,15 +911,11 @@
 	}
 	if (bRet)
 	{
-		if (pszFilters)
-			delete [] pszFilters;
 		path = CString(ofn.lpstrFile);
 		if (filterindex)
 			*filterindex = ofn.nFilterIndex;
 		return true;
 	}
-	if (pszFilters)
-		delete [] pszFilters;
 	return false;
 }
 
Index: TortoiseProc/Commands/CheckoutCommand.cpp
===================================================================
--- TortoiseProc/Commands/CheckoutCommand.cpp	(revision 17027)
+++ TortoiseProc/Commands/CheckoutCommand.cpp	(working copy)
@@ -23,6 +23,7 @@
 #include "SVNProgressDlg.h"
 #include "BrowseFolder.h"
 #include "MessageBox.h"
+#include "auto_buffer.h"
 
 bool CheckoutCommand::Execute()
 {
@@ -39,7 +40,7 @@
 		{
 			checkoutDirectory.SetFromWin(sOrigCWD, true);
 			DWORD len = ::GetTempPath(0, NULL);
-			TCHAR * tszPath = new TCHAR[len];
+			auto_buffer<TCHAR> tszPath(len);
 			::GetTempPath(len, tszPath);
 			if (_tcsncicmp(checkoutDirectory.GetWinPath(), tszPath, len-2 /* \\ and \0 */) == 0)
 			{
@@ -47,7 +48,6 @@
 				// we don't use that but leave it empty instead.
 				checkoutDirectory.Reset();
 			}
-			delete [] tszPath;
 		}
 		else
 		{
Index: TortoiseProc/Commands/CreatePatchCommand.cpp
===================================================================
--- TortoiseProc/Commands/CreatePatchCommand.cpp	(revision 17027)
+++ TortoiseProc/Commands/CreatePatchCommand.cpp	(working copy)
@@ -26,6 +26,7 @@
 #include "SVN.h"
 #include "TempFile.h"
 #include "ProgressDlg.h"
+#include "auto_buffer.h"
 
 #define PATCH_TO_CLIPBOARD_PSEUDO_FILENAME		_T(".TSVNPatchToClipboard")
 
@@ -94,7 +95,7 @@
 
 		CString sFilter;
 		sFilter.LoadString(IDS_PATCHFILEFILTER);
-		TCHAR * pszFilters = new TCHAR[sFilter.GetLength()+4];
+		auto_buffer<TCHAR> pszFilters(sFilter.GetLength()+4);
 		_tcscpy_s (pszFilters, sFilter.GetLength()+4, sFilter);
 		// Replace '|' delimiters with '\0's
 		TCHAR *ptr = pszFilters + _tcslen(pszFilters);  //set ptr at the NULL
@@ -109,10 +110,8 @@
 		// Display the Open dialog box. 
 		if (GetSaveFileName(&ofn)==FALSE)
 		{
-			delete [] pszFilters;
 			return FALSE;
 		}
-		delete [] pszFilters;
 		savePath = CTSVNPath(ofn.lpstrFile);
 		if (ofn.nFilterIndex == 1)
 		{
Index: TortoiseProc/Commands/DelUnversionedCommand.cpp
===================================================================
--- TortoiseProc/Commands/DelUnversionedCommand.cpp	(revision 17027)
+++ TortoiseProc/Commands/DelUnversionedCommand.cpp	(working copy)
@@ -18,8 +18,8 @@
 //
 #include "StdAfx.h"
 #include "DelUnversionedCommand.h"
-
 #include "DeleteUnversionedDlg.h"
+#include "auto_buffer.h"
 
 bool DelUnversionedCommand::Execute()
 {
@@ -40,7 +40,7 @@
 		}
 		filelist += _T("|");
 		int len = filelist.GetLength();
-		TCHAR * buf = new TCHAR[len+2];
+		auto_buffer<TCHAR> buf(len+2);
 		_tcscpy_s(buf, len+2, filelist);
 		for (int i=0; i<len; ++i)
 			if (buf[i] == '|')
@@ -53,7 +53,6 @@
 		fileop.fFlags = FOF_NO_CONNECTED_ELEMENTS | FOF_ALLOWUNDO;
 		fileop.lpszProgressTitle = _T("deleting file");
 		bRet = (SHFileOperation(&fileop) == 0);
-		delete [] buf;
 	}
 	return true;
 }
Index: TortoiseProc/CommitDlg.cpp
===================================================================
--- TortoiseProc/CommitDlg.cpp	(revision 17027)
+++ TortoiseProc/CommitDlg.cpp	(working copy)
@@ -31,6 +31,7 @@
 #include "SVNStatus.h"
 #include "HistoryDlg.h"
 #include "Hooks.h"
+#include "auto_buffer.h"
 
 #ifdef _DEBUG
 #define new DEBUG_NEW
@@ -1153,7 +1154,7 @@
 			return;
 		}
 		// allocate memory to hold file contents
-		char * buffer = new char[size];
+		auto_buffer<char> buffer(size);
 		DWORD readbytes;
 		ReadFile(hFile, buffer, size, &readbytes, NULL);
 		CloseHandle(hFile);
@@ -1161,7 +1162,6 @@
 		IsTextUnicode(buffer, readbytes, &opts);
 		if (opts & IS_TEXT_UNICODE_NULL_BYTES)
 		{
-			delete [] buffer;
 			return;
 		}
 		if (opts & IS_TEXT_UNICODE_UNICODE_MASK)
@@ -1170,14 +1170,12 @@
 		}
 		if ((opts & IS_TEXT_UNICODE_NOT_UNICODE_MASK)||(opts == 0))
 		{
-			int ret = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (LPCSTR)buffer, readbytes, NULL, 0);
-			wchar_t * pWideBuf = new wchar_t[ret];
-			int ret2 = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (LPCSTR)buffer, readbytes, pWideBuf, ret);
+			const int ret = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (LPCSTR)buffer, readbytes, NULL, 0);
+			auto_buffer<wchar_t> pWideBuf(ret);
+			const int ret2 = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (LPCSTR)buffer, readbytes, pWideBuf, ret);
 			if (ret2 == ret)
 				sFileContent = wstring(pWideBuf, ret);
-			delete [] pWideBuf;
 		}
-		delete [] buffer;
 	}
 	if (sFileContent.empty()|| !m_bRunThread)
 	{
Index: TortoiseProc/EditPropertiesDlg.cpp
===================================================================
--- TortoiseProc/EditPropertiesDlg.cpp	(revision 17027)
+++ TortoiseProc/EditPropertiesDlg.cpp	(working copy)
@@ -29,8 +29,8 @@
 #include "ProgressDlg.h"
 #include "InputLogDlg.h"
 #include "XPTheme.h"
+#include "auto_buffer.h"
 
-
 IMPLEMENT_DYNAMIC(CEditPropertiesDlg, CResizableStandAloneDialog)
 
 CEditPropertiesDlg::CEditPropertiesDlg(CWnd* pParent /*=NULL*/)
@@ -666,14 +666,14 @@
 			}
 			if (fread(&nNameBytes, sizeof(int), 1, stream) == 1)
 			{
-				TCHAR * pNameBuf = new TCHAR[nNameBytes/sizeof(TCHAR)];
+				auto_buffer<TCHAR> pNameBuf(nNameBytes/sizeof(TCHAR));
 				if (fread(pNameBuf, 1, nNameBytes, stream) == (size_t)nNameBytes)
 				{
 					CString sName = CString(pNameBuf, nNameBytes/sizeof(TCHAR));
 					int nValueBytes = 0;
 					if (fread(&nValueBytes, sizeof(int), 1, stream) == 1)
 					{
-						BYTE * pValueBuf = new BYTE[nValueBytes];
+						auto_buffer<BYTE> pValueBuf(nValueBytes);
 						if (fread(pValueBuf, sizeof(char), nValueBytes, stream) == (size_t)nValueBytes)
 						{
 							std::string propertyvalue;
@@ -718,7 +718,6 @@
 							CMessageBox::Show(m_hWnd, IDS_EDITPROPS_ERRIMPORTFORMAT, IDS_APPNAME, MB_ICONERROR);
 							bFailed = true;
 						}
-						delete [] pValueBuf;
 					}
 					else
 					{
@@ -733,7 +732,6 @@
 					CMessageBox::Show(m_hWnd, IDS_EDITPROPS_ERRIMPORTFORMAT, IDS_APPNAME, MB_ICONERROR);
 					bFailed = true;
 				}
-				delete [] pNameBuf;
 			}
 			else
 			{
Index: TortoiseProc/EditPropertyValueDlg.cpp
===================================================================
--- TortoiseProc/EditPropertyValueDlg.cpp	(revision 17027)
+++ TortoiseProc/EditPropertyValueDlg.cpp	(working copy)
@@ -23,8 +23,8 @@
 #include "AppUtils.h"
 #include "StringUtils.h"
 #include "EditPropertyValueDlg.h"
+#include "auto_buffer.h"
 
-
 IMPLEMENT_DYNAMIC(CEditPropertyValueDlg, CResizableStandAloneDialog)
 
 CEditPropertyValueDlg::CEditPropertyValueDlg(CWnd* pParent /*=NULL*/)
@@ -376,12 +376,11 @@
 		DWORD size = GetFileSize(hFile, NULL);
 		FILE * stream;
 		_tfopen_s(&stream, openPath, _T("rbS"));
-		char * buf = new char[size];
+		auto_buffer<char> buf(size);
 		if (fread(buf, sizeof(char), size, stream)==size)
 		{
 			m_PropValue.assign(buf, size);
 		}
-		delete [] buf;
 		fclose(stream);
 		// see if the loaded file contents are binary
 		SetPropertyValue(m_PropValue);
Index: TortoiseProc/TortoiseProc.cpp
===================================================================
--- TortoiseProc/TortoiseProc.cpp	(revision 17027)
+++ TortoiseProc/TortoiseProc.cpp	(working copy)
@@ -42,6 +42,7 @@
 #include "..\version.h"
 #include "JumpListHelpers.h"
 #include "CmdUrlParser.h"
+#include "auto_buffer.h"
 
 #define APPID (_T("TSVN.TSVN.1") _T(TSVN_PLATFORM))
 
@@ -295,13 +296,12 @@
 		DWORD len = GetCurrentDirectory(0, NULL);
 		if (len)
 		{
-			TCHAR * originalCurrentDirectory = new TCHAR[len];
+			auto_buffer<TCHAR> originalCurrentDirectory(len);
 			if (GetCurrentDirectory(len, originalCurrentDirectory))
 			{
 				sOrigCWD = originalCurrentDirectory;
 				sOrigCWD = CPathUtils::GetLongPathname(sOrigCWD);
 			}
-			delete [] originalCurrentDirectory;
 		}
 		TCHAR pathbuf[MAX_PATH];
 		GetTempPath(MAX_PATH, pathbuf);
@@ -394,7 +394,7 @@
 	// apps might still be needing the recent ones.
 	{
 		DWORD len = ::GetTempPath(0, NULL);
-		TCHAR * path = new TCHAR[len + 100];
+		auto_buffer<TCHAR> path(len + 100);
 		len = ::GetTempPath (len+100, path);
 		if (len != 0)
 		{
@@ -424,7 +424,6 @@
 				}
 			}
 		}	
-		delete[] path;		
 	}
 
 
Index: TortoiseShell/ContextMenu.cpp
===================================================================
--- TortoiseShell/ContextMenu.cpp	(revision 17027)
+++ TortoiseShell/ContextMenu.cpp	(working copy)
@@ -23,6 +23,7 @@
 #include "UnicodeUtils.h"
 #include "SVNProperties.h"
 #include "SVNStatus.h"
+#include "auto_buffer.h"
 
 #define GetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[0])
 #define GetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[i+1])
@@ -249,14 +250,12 @@
 					UINT len = DragQueryFile(drop, i, NULL, 0);
 					if (len == 0)
 						continue;
-					TCHAR * szFileName = new TCHAR[len+1];
+					auto_buffer<TCHAR> szFileName(len+1);
 					if (0 == DragQueryFile(drop, i, szFileName, len+1))
 					{
-						delete [] szFileName;
 						continue;
 					}
 					tstring str = tstring(szFileName);
-					delete [] szFileName;
 					if ((str.empty() == false)&&(g_ShellCache.IsContextPathAllowed(szFileName)))
 					{
 						if (itemStates & ITEMIS_ONLYONE)
@@ -746,8 +745,8 @@
 	//for TortoiseProc.exe to read out again.
 	DWORD written = 0;
 	DWORD pathlength = GetTempPath(0, NULL);
-	TCHAR * path = new TCHAR[pathlength+1];
-	TCHAR * tempFile = new TCHAR[pathlength + 100];
+	auto_buffer<TCHAR> path(pathlength+1);
+	auto_buffer<TCHAR> tempFile(pathlength + 100);
 	GetTempPath (pathlength+1, path);
 	GetTempFileName (path, _T("svn"), 0, tempFile);
 	tempfile = tstring(tempFile);
@@ -760,8 +759,6 @@
 		FILE_ATTRIBUTE_TEMPORARY,
 		0);
 
-	delete [] path;
-	delete [] tempFile;
 	if (file == INVALID_HANDLE_VALUE)
 		return false;
 
@@ -800,8 +797,8 @@
 	//write all selected files and paths to a temporary file
 	//for TortoiseProc.exe to read out again.
 	DWORD pathlength = GetTempPath(0, NULL);
-	TCHAR * path = new TCHAR[pathlength+1];
-	TCHAR * tempFile = new TCHAR[pathlength + 100];
+	auto_buffer<TCHAR> path(pathlength+1);
+	auto_buffer<TCHAR> tempFile(pathlength + 100);
 	GetTempPath (pathlength+1, path);
 	GetTempFileName (path, _T("svn"), 0, tempFile);
 	tstring retFilePath = tstring(tempFile);
@@ -814,8 +811,6 @@
 								FILE_ATTRIBUTE_TEMPORARY,
 								0);
 
-	delete [] path;
-	delete [] tempFile;
 	if (file == INVALID_HANDLE_VALUE)
 		return tstring();
 		
@@ -1665,13 +1660,11 @@
 						LPCSTR lpstr = (LPCSTR)GlobalLock(hglb); 
 
 						DWORD len = GetTempPath(0, NULL);
-						TCHAR * path = new TCHAR[len+1];
-						TCHAR * tempF = new TCHAR[len+100];
+						auto_buffer<TCHAR> path(len+1);
+						auto_buffer<TCHAR> tempF(len+100);
 						GetTempPath (len+1, path);
 						GetTempFileName (path, TEXT("svn"), 0, tempF);
 						std::wstring sTempFile = std::wstring(tempF);
-						delete [] path;
-						delete [] tempF;
 
 						FILE * outFile;
 						size_t patchlen = strlen(lpstr);
Index: TortoiseShell/ShellExt.cpp
===================================================================
--- TortoiseShell/ShellExt.cpp	(revision 17027)
+++ TortoiseShell/ShellExt.cpp	(working copy)
@@ -28,6 +28,7 @@
 #include "ShellObjects.h"
 #include "..\version.h"
 #include "libintl.h"
+#include "auto_buffer.h"
 #undef swprintf
 
 extern ShellObjects g_shellObjects;
@@ -212,10 +213,9 @@
 	do 
 	{
 		bufferlen += MAX_PATH;		// MAX_PATH is not the limit here!
-		TCHAR * pBuf = new TCHAR[bufferlen];
+		auto_buffer<TCHAR> pBuf(bufferlen);
 		len = GetModuleFileName(g_hmodThisDll, pBuf, bufferlen);	
 		path = tstring(pBuf, len);
-		delete [] pBuf;
 	} while(len == bufferlen);
 	path = path.substr(0, path.rfind('\\') + 1);
 
Index: TortoiseShell/SVNPropertyPage.cpp
===================================================================
--- TortoiseShell/SVNPropertyPage.cpp	(revision 17027)
+++ TortoiseShell/SVNPropertyPage.cpp	(working copy)
@@ -23,6 +23,7 @@
 #include "UnicodeUtils.h"
 #include "PathUtils.h"
 #include "SVNStatus.h"
+#include "auto_buffer.h"
 
 #define MAX_STRING_LENGTH		4096			//should be big enough
 
@@ -188,8 +189,8 @@
 					if (LOWORD(wParam) == IDC_EDITPROPERTIES)
 					{
 						DWORD pathlength = GetTempPath(0, NULL);
-						TCHAR * path = new TCHAR[pathlength+1];
-						TCHAR * tempFile = new TCHAR[pathlength + 100];
+						auto_buffer<TCHAR> path(pathlength+1);
+						auto_buffer<TCHAR> tempFile(pathlength + 100);
 						GetTempPath (pathlength+1, path);
 						GetTempFileName (path, _T("svn"), 0, tempFile);
 						tstring retFilePath = tstring(tempFile);
@@ -202,8 +203,6 @@
 							FILE_ATTRIBUTE_TEMPORARY,
 							0);
 
-						delete [] path;
-						delete [] tempFile;
 						if (file != INVALID_HANDLE_VALUE)
 						{
 							DWORD written = 0;
@@ -301,7 +300,7 @@
 				if (svn.status->entry->url)
 				{
 					size_t len = strlen(svn.status->entry->url);
-					char * unescapedurl = new char[len+1];
+					auto_buffer<char> unescapedurl(len+1);
 					strcpy_s(unescapedurl, len+1, svn.status->entry->url);
 					CPathUtils::Unescape(unescapedurl);
 					SetDlgItemText(m_hwnd, IDC_REPOURL, UTF8ToWide(unescapedurl).c_str());
@@ -316,7 +315,6 @@
 						ShowWindow(GetDlgItem(m_hwnd, IDC_ESCAPEDURLLABEL), SW_HIDE);
 						ShowWindow(GetDlgItem(m_hwnd, IDC_REPOURLUNESCAPED), SW_HIDE);
 					}
-					delete [] unescapedurl;
 				}
 				else
 				{
Index: Utils/auto_buffer.h
===================================================================
--- Utils/auto_buffer.h	(revision 17027)
+++ Utils/auto_buffer.h	(working copy)
@@ -73,9 +73,9 @@
         return temp;
     }
 
-    void reset (size_t size = 0)
+    void reset (size_t newSize = 0)
     {
         delete[] buffer;
-        buffer = (size == 0 ? NULL : new T[size]);
+        buffer = (newSize == 0 ? NULL : new T[newSize]);
     }
 };
Index: Utils/PathUtils.cpp
===================================================================
--- Utils/PathUtils.cpp	(revision 17027)
+++ Utils/PathUtils.cpp	(working copy)
@@ -23,9 +23,10 @@
 
 BOOL CPathUtils::MakeSureDirectoryPathExists(LPCTSTR path)
 {
-	size_t len = _tcslen(path);
-	TCHAR * buf = new TCHAR[len+10];
-	TCHAR * internalpathbuf = new TCHAR[len+10];
+	const size_t len = _tcslen(path);
+	const size_t fullLen = len+10;
+	auto_buffer<TCHAR> buf(fullLen);
+	auto_buffer<TCHAR> internalpathbuf(fullLen);
 	TCHAR * pPath = internalpathbuf;
 	SECURITY_ATTRIBUTES attribs;
 
@@ -34,24 +35,22 @@
 	attribs.nLength = sizeof(SECURITY_ATTRIBUTES);
 	attribs.bInheritHandle = FALSE;
 
-	ConvertToBackslash(internalpathbuf, path, len+10);
+	ConvertToBackslash(internalpathbuf, path, fullLen);
 	if (_tcsncmp(internalpathbuf, _T("\\\\?\\"), 4) == 0)
 		pPath += 4;
 	do
 	{
-		SecureZeroMemory(buf, (len+10)*sizeof(TCHAR));
+		SecureZeroMemory(buf, fullLen*sizeof(TCHAR));
 		TCHAR * slashpos = _tcschr(pPath, '\\');
 		if (slashpos)
-			_tcsncpy_s(buf, len+10, internalpathbuf, slashpos - internalpathbuf);
+			_tcsncpy_s(buf, fullLen, internalpathbuf, slashpos - internalpathbuf);
 		else
-			_tcsncpy_s(buf, len+10, internalpathbuf, len+10);
+			_tcsncpy_s(buf, fullLen, internalpathbuf, fullLen);
 		CreateDirectory(buf, &attribs);
 		pPath = _tcschr(pPath, '\\');
 	} while ((pPath++)&&(_tcschr(pPath, '\\')));
 	
-	BOOL bRet = CreateDirectory(internalpathbuf, &attribs);
-	delete[] buf;
-	delete[] internalpathbuf;
+	const BOOL bRet = CreateDirectory(internalpathbuf, &attribs);
 	return bRet;
 }
 
@@ -279,29 +278,26 @@
 		ret = GetFullPathName(path, 0, NULL, NULL);
 		if (ret)
 		{
-			TCHAR * pathbuf = new TCHAR[ret+1];
+			auto_buffer<TCHAR> pathbuf(ret+1);
 			if ((ret = GetFullPathName(path, ret, pathbuf, NULL))!=0)
 			{
 				sRet = CString(pathbuf, ret);
 			}
-			delete [] pathbuf;
 		}
 	}
 	else if (PathCanonicalize(pathbufcanonicalized, path))
 	{
 		ret = ::GetLongPathName(pathbufcanonicalized, NULL, 0);
-		TCHAR * pathbuf = new TCHAR[ret+2];	
+		auto_buffer<TCHAR> pathbuf(ret+2);
 		ret = ::GetLongPathName(pathbufcanonicalized, pathbuf, ret+1);
 		sRet = CString(pathbuf, ret);
-		delete[] pathbuf;
 	}
 	else
 	{
 		ret = ::GetLongPathName(path, NULL, 0);
-		TCHAR * pathbuf = new TCHAR[ret+2];
+		auto_buffer<TCHAR> pathbuf(ret+2);
 		ret = ::GetLongPathName(path, pathbuf, ret+1);
 		sRet = CString(pathbuf, ret);
-		delete[] pathbuf;
 	}
 	if (ret == 0)
 		return path;
@@ -391,13 +387,11 @@
 
 	patha = PathUnescape(patha);
 
-	WCHAR * bufw;
 	len = patha.GetLength();
-	bufw = new WCHAR[len*4 + 1];
+	auto_buffer<WCHAR> bufw(len*4 + 1);
 	SecureZeroMemory(bufw, (len*4 + 1)*sizeof(WCHAR));
 	MultiByteToWideChar(CP_UTF8, 0, patha, -1, bufw, len*4);
 	CStringW ret = CStringW(bufw);
-	delete [] bufw;
 	return ret;
 }
 
