Permalink
Browse files

Fixed (?) bug where notepad windows might load garbage

  • Loading branch information...
1 parent 0665664 commit 24e3b5253cd5c69a004e700d3f5a4815666704e3 @nickgammon committed Sep 26, 2010
Showing with 36 additions and 3 deletions.
  1. +1 −1 TextDocument.cpp
  2. +35 −2 TextView.cpp
View
@@ -115,7 +115,7 @@ void CTextDocument::Dump(CDumpContext& dc) const
void CTextDocument::Serialize(CArchive& ar)
{
// CEditView contains an edit control which handles all serialization
- ((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
+ ((CTextView*)m_viewList.GetHead())->SerializeRaw(ar);
if (ar.IsLoading ())
CreateMonitoringThread (ar.GetFile ()->GetFilePath ());
View
@@ -238,9 +238,42 @@ void CTextView::SerializeRaw(CArchive& ar)
{
CFile* pFile = ar.GetFile();
ASSERT(pFile->GetPosition() == 0);
- DWORD nFileSize = pFile->GetLength();
+ DWORD nLen = pFile->GetLength();
// ReadFromArchive takes the number of characters as argument
- ReadFromArchive(ar, (UINT)nFileSize/sizeof(TCHAR));
+
+ // REPLACING: ReadFromArchive(ar, (UINT)nFileSize/sizeof(TCHAR));
+
+ // with this (we got garbage at times):
+
+ LPVOID hText = LocalAlloc(LMEM_MOVEABLE, (nLen+1)*sizeof(TCHAR));
+ if (hText == NULL)
+ AfxThrowMemoryException();
+
+ LPTSTR lpszText = (LPTSTR)LocalLock(hText);
+ ASSERT(lpszText != NULL);
+ if (ar.Read(lpszText, nLen*sizeof(TCHAR)) != nLen*sizeof(TCHAR))
+ {
+ LocalUnlock(hText);
+ LocalFree(hText);
+ AfxThrowArchiveException(CArchiveException::endOfFile);
+ }
+ // Replace the editing edit buffer with the newly loaded data
+ lpszText[nLen] = '\0';
+
+ // set the text with SetWindowText, then free
+ BOOL bResult = ::SetWindowText(m_hWnd, lpszText);
+ LocalUnlock(hText);
+ LocalFree(hText);
+
+ // make sure that SetWindowText was successful
+ if (!bResult || ::GetWindowTextLength(m_hWnd) < (int)nLen)
+ AfxThrowMemoryException();
+
+ // remove old shadow buffer
+ delete[] m_pShadowBuffer;
+ m_pShadowBuffer = NULL;
+ m_nShadowSize = 0;
+
}
ASSERT_VALID(this);
} /* end of CTextView::SerializeRaw */

0 comments on commit 24e3b52

Please sign in to comment.