SetClipboard() crashes when called twice 
Author Message
 SetClipboard() crashes when called twice

Hello All,
I have an MFC DLL in which I'm trying to implement copy/cut/past
functionality. The copying is done from a CDialog derived class. My
problem is this:
After the first COleDataSource::SetClipboard() call, all the following
calls to SetClipboard() cause a crash in CoRegisterChannelHook()
called by OleSetClipboard().

This is the code segment I'm using:

     m_pOLEDataSource->Empty();
     m_pOLEDataSource->DelayRenderData(m_ClipBoardFormat);
     m_pOLEDataSource->SetClipboard();

where "m_pOLEDataSource" is a pointer that is allocated once and is
reused in all succeeding Cut/Copy operations.

Any ideas? Do I have to create a new OleDataSouce for every copy/cut
operation?

Thank you in advanced.

AG



Sun, 24 Apr 2005 19:42:46 GMT  
 SetClipboard() crashes when called twice

Quote:

>Hello All,
>I have an MFC DLL in which I'm trying to implement copy/cut/past
>functionality. The copying is done from a CDialog derived class. My
>problem is this:
>After the first COleDataSource::SetClipboard() call, all the following
>calls to SetClipboard() cause a crash in CoRegisterChannelHook()
>called by OleSetClipboard().

>This is the code segment I'm using:

>     m_pOLEDataSource->Empty();
>     m_pOLEDataSource->DelayRenderData(m_ClipBoardFormat);
>     m_pOLEDataSource->SetClipboard();

>where "m_pOLEDataSource" is a pointer that is allocated once and is
>reused in all succeeding Cut/Copy operations.

When you call SetClipboard, you typically transfer ownership of the
object to OLE. Thus, the second time you call SetClipboard and
thereafter, OLE ends up destroying the object currently on the
clipboard, which is the object you are reusing.

Quote:
>Any ideas? Do I have to create a new OleDataSouce for every copy/cut
>operation?

Yep. See this article for more:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcco...

See also the docs for OleSetClipboard, OleFlushClipboard, etc., as
they go into much more detail than the MFC docs.

P.S. Since you're using delayed rendering, you must call
COleDataSource::FlushClipboard anytime the underlying data might go
away, such as when quitting your app, assuming you want to leave the
data on the clipboard. Otherwise, you can call OleSetClipboard(0).

--
Doug Harrison
Microsoft MVP - Visual C++



Mon, 25 Apr 2005 06:06:34 GMT  
 SetClipboard() crashes when called twice
Doug,
Thanks for the response. Here is another question:
If I do something like this:

void MyDialog::PutDataOnClipBoard(){
  COleDataSource *pSource=new COleDataSource;

  pSource->DelayRenderData(m_ClipboardFormat);
  pSource->SetClipboard();

Quote:
}

Who will delete the pSource object?

Thanks

Ali



Mon, 25 Apr 2005 20:43:05 GMT  
 SetClipboard() crashes when called twice

Quote:

>Doug,
>Thanks for the response. Here is another question:
>If I do something like this:

>void MyDialog::PutDataOnClipBoard(){
>  COleDataSource *pSource=new COleDataSource;

>  pSource->DelayRenderData(m_ClipboardFormat);
>  pSource->SetClipboard();

>}

>Who will delete the pSource object?

Roughly speaking, OLE will, when a new object is stored on the
clipboard, or when OleSetClipboard(0) is called to empty the
clipboard. IIRC, COleDataSource implements the IDataObject COM
interface, and SetClipboard hands off the object to OLE, which
increments the object's reference count. When the clipboard contents
are replaced, OLE decrements its current IDataObject's reference
count, which will normally drop it to zero, causing your
COleDataSource to "delete this". Thus, OLE causes your object to
delete itself.

--
Doug Harrison
Microsoft MVP - Visual C++



Tue, 26 Apr 2005 05:22:05 GMT  
 SetClipboard() crashes when called twice
Doug,
Thank you for the explanation. This explains how I managed to get the
code to work by allocating a new "COleDataSource" before putting it on
the clipboard. But since I couldn't delete the object after the paste
operation was complete, I assumed that I was doing something wrong.
After spending countless hours in the past chasing after memory leaks,
I'm a little "gun-shy".

Take Care,

-AG



Tue, 26 Apr 2005 17:55:02 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. 2nd SetClipboard call crashes app in XP, not in 2K/98

2. Application crashes after calling GetPrinterDeviceDefaults twice in Win9x

3. Please Help! SetClipboard() crashes WinXP in ole32.dll

4. SetClipboard() crashes WinXP

5. Danger in Calling GC::SuppressFinalize(this) twice?

6. Server.URLEncode needs to be called twice?

7. MDI maximized, OnSize called twice

8. ATL Server ValidateAndExchange function getting called twice

9. My keyboard hook gets called twice.

10. calling to read() twice, once within a nested loop

11. revised code of calling a function twice from printf

12. revised code of calling a function twice from printf

 

 
Powered by phpBB® Forum Software