FIX: Clipboard Assistant - Paste Fails or Causes Hard Break

Last reviewed: September 19, 1997
Article ID: Q148687
The information in this article applies to:
  • Microsoft Visual C++, 32-bit Edition, versions 4.0, 4.1, 4.2

SYMPTOMS

Code generated by the Clipboard Assistant for a Custom Clipboard Format may cause the following problems:

  • Copying to the clipboard fails to replace what was previously placed on the clipboard.
  • An access violation occurs with a message similar to the following:

    Exception: access violation (0xc0000005), Address: 0x5f82cf37

  • An unexpected memory overwrite occurs after several clipboard operations.
  • On the Checked build of Windows NT, a Cut, Copy, or Paste operation causes a hard-coded breakpoint to be encountered and a trace message similar to the following to be generated:

    BASE: GlobalFree called with a locked object.

CAUSE

The first three problems can occur because of the OnEditPaste function generated by the Clipboard Assistant. The code is not correct because it frees the HGLOBAL that it retrieves from the clipboard when doing a paste operation. This memory should not be freed because the Clipboard will free it when necessary. The code also fails to call CloseClipboard for all possible calls to OpenClipboard.

The fourth problem is caused by a separate bug in the CSharedFile::Detach() function. For more details on this problem, please see the following article in the Microsoft Knowledge Base:

  ARTICLE-ID: Q148455
  TITLE     : BUG: CSharedFile::Detach() Does Not Call GlobalUnlock()

RESOLUTION

The sample code in this article shows new versions of the OnEditCopy, OnEditCut, and OnEditPaste functions. You should use this code to replace the versions previously generated by the Clipboard Assistant.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem has been fixed in Visual C++ version 5.0.

MORE INFORMATION

Sample Code to Resolve Problem

   /* Compile options needed: Default AppWizard options  */

   void CMyView::OnEditCopy()
   {
   // CG: This block was added by the Clipboard Assistant component
     {
       CSharedFile memFile;
       CArchive ar(&memFile, CArchive::store|CArchive::bNoFlushOnDelete);

       // serialize data to archive object
       DoCutCopyPaste(ar, FALSE);

       ar.Flush();

       HGLOBAL hData = memFile.Detach();
       //Resolution for CSharedFile::Detach() problem
       //Check Q148455 for current status.
       // _MFC_VER might need to be updated.
       #if _MFC_VER <= 0x0420
         ::GlobalUnlock(hData);
       #endif

       if (OpenClipboard())
       {
         ::SetClipboardData(m_nClipboardFormat, hData);
         CloseClipboard();
       }
       else
         AfxMessageBox(CG_IDS_CANNOT_OPEN_CLIPBOARD);
     }
   }

   void CMyView::OnEditCut()
   {
     // CG: This block was added by the Clipboard Assistant component
     {
       CSharedFile memFile;
       CArchive ar(&memFile, CArchive::store|CArchive::bNoFlushOnDelete);

       // Serialize data to archive object
       DoCutCopyPaste(ar, TRUE);

       ar.Flush();

       HGLOBAL hData = memFile.Detach();

       //Resolution for CSharedFile::Detach() problem
       //Check Q148455 for current status.
       // _MFC_VER might need to be updated.
       #if _MFC_VER <= 0x0420
         ::GlobalUnlock(hData);
       #endif

       if (OpenClipboard())
       {
         ::SetClipboardData(m_nClipboardFormat, hData);
         CloseClipboard();
       }
       else
         AfxMessageBox(CG_IDS_CANNOT_OPEN_CLIPBOARD);
     }
   }

   void CMyView::OnEditPaste()
   {
     // CG: This block was added by the Clipboard Assistant component
     {
       if (OpenClipboard())
       {
         HANDLE hData = ::GetClipboardData(m_nClipboardFormat);
         if (hData != NULL)
         {
           CSharedFile memFile;
           memFile.SetHandle(hData,FALSE);
           CArchive ar(&memFile, CArchive::load);

           // Serialize data to document
           DoCutCopyPaste(ar, FALSE);
           ar.Close();

           //Resolution for CSharedFile::Detach() problem
           //Check Q148455 for current status.
           // _MFC_VER might need to be updated.
           #if _MFC_VER <= 0x0420
             ::GlobalUnlock(memFile.Detach());
           #else
             memFile.Detach();
           #endif
         }
         else
           AfxMessageBox(CG_IDS_CANNOT_GET_CLIPBOARD_DATA);

         CloseClipboard();
       }
       else
         AfxMessageBox(CG_IDS_CANNOT_OPEN_CLIPBOARD);
     }
   }

Keywords          : VwbIss kbcode kbprg kbtool kbbuglist kbfixlist
Technology        : kbMfc
Version           : 4.0 4.1 4.2
Platform          : NT WINDOWS
Issue type        : kbbug
Solution Type     : kbfix


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 19, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.