Help and Support
 

powered byLive Search

PRB: Drag and Drop with TYMED_FILE Runs Out of File Handles

Retired KB ArticleThis article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Article ID:135682
Last Review:December 8, 2003
Revision:2.0
This article was previously published under Q135682
On This Page

SYMPTOMS

Calling Open() on the CFile pointer returned from COleDataObject::GetFileData() may return EMFILE. Usually, this will happen after repeated drag and drop operations and subsequent calls to GetFileData(). It is also possible to see memory leaks upon termination of the application, specifically, of memory blocks the size of a CFile object.

Back to the top

CAUSE

The documentation for COleDataObject::GetFileData() does not mention that the returned CFile pointer is owned by the caller. Therefore, it is the responsibility of the caller to call Close() on or simply delete that pointer. The reason for the EMFILE return code is that the files opened in the drag and drop operation are never closed and therefore the system handles are exhausted. Memory leaks are caused by not deleting the CFile objects that were dynamically allocated by GetFileData().

The documentation for COleDataObject::GetFileData provided with Visual C++ 4.0 has been improved. It mentions the fact that it is the responsibility of the caller to delete the returned CFile object, thereby closing the file.

Back to the top

RESOLUTION

Remember to delete the CFile pointer returned from GetFileData() after using it.

Back to the top

Sample Code

The following OnDrop() function definition illustrates the correct cleanup after using the CFile pointer returned from GetFileData().
   BOOL CMyView::OnDrop(COleDataObject* pDataObject, DROPEFFECT de,
                        CPoint point)
   {
       CFile * pFile = pDataObject->GetFileData(CF_HGLOBAL);
       char lpBuf[100];

       if (NULL != pFile)
           pFile->Read(lpBuf, 100);
       .
       .
       .
       // Make sure to delete the CFile *
       delete pFile;
       return TRUE;
   }
				

Back to the top


APPLIES TO
Microsoft Visual C++ 1.5 Professional Edition
Microsoft Visual C++ 1.51
Microsoft Visual C++ 1.52 Professional Edition
Microsoft Visual C++ 2.0 Professional Edition
Microsoft Visual C++ 2.1
Microsoft Visual C++ 2.2
Microsoft Visual C++ 4.0 Standard Edition

Back to the top

Keywords: 
kbdragdrop kbprb KB135682

Back to the top

Article Translations

 

Other Support Options

  • Need More Help?
    Contact a Support professional by E-mail, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.