views:

229

answers:

1

I have implemented my custom drag&drop by deriving from COleDataSource and COleDropTarget. Everythings work fine but I have an scenario that makes the application crashes.

That happens when the dialog where the drag&drop controls are placed is destroyed while the user is in the middle of a drag&drop operation. This is not usual because normally to close a dialog the user have to use the mouse or the keyboard and then the drag&drop operation is automatically canceled. But in this scenario the dialog can be closed due to an external condition (a custom message that changes the view) and then the drap&drop operation is not canceled.

So my question is, how can I programmatically cancel the drag&drop operation? The operation is started from the OnLButtonDown message handle calling COleDataSource::DoDragDrop.

Update:

I've implemented using DelayRender and it continues crashing. These are the two asserts I am getting:

  1. When the mouse pointer (while dragging) is inside a control that is a COleDropTarget-derived class.

alt text

The assert is in the Revoke method and this is the line:

ASSERT(m_lpDataObject == NULL);
  1. When the mouse pointer is not inside a COleDropTarget-derived class.

alt text

In this case the assert is in the destruction of the COleDataSource (in fact it is in its parent class CCmdTarget). In this line:

ASSERT(m_dwRef <= 1);

Thanks in advance!

+1  A: 

I'm not sure if this would work but you could try overriding the QueryContinueDrag method of the COleDropTarget instance and returning DRAGDROP_S_CANCEL in the case where the dialog has been closed

Karim
I already tried that and it didn't work. QueryContinueDrag is only called when there is a change in keyboard or mouse state and it is not could after OnDestroy (in the dragged/dropped control) and the actual destruction so I can not figure out how to use it.
Javier De Pedro
At the end I used QueryContinueDrag to fix my problem. The problem at the beginning was that I tried to cancel the drag operation once the control was being destroyed. The solution is not to destroy the control until the drag operation is cancel...I set a timer and wait until the cancellation before trying to destroy it.
Javier De Pedro