Thursday, August 28, 2008

RaceOnRCWCleanup

RaceOnRCWCleanup

This is a discussion from a MS programmer: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=707167&SiteID=1

he says to look at the callstack when the MDA (managed debugging assistant) fires. This should tell me which RCW is being freed.

The call to release the RCW is Marshal.ReleaseCOmObject. This is being done while the RCW is still being reference in the stack of the current thread or in another thread.

Could be on of the following:

  1. RCW blocked on an outgoing call and a re-entrancy occured while is was block
  2. asynchronous call occured on a background thread.
There is not safe way to release a RCW while it is in the middle of a call. Doing this will most likely result in a crash.

May need to do one of the following:

  1. cancel the data transfer and wait
  2. to avoid re-entrancy more the data transfer to a background thread so I can control the canceling (this may be my problem)
To help the bugger work in this case I need to
  1. disable "Enable Just My Code" under Tools Options (Debugging)
  2. Enable unmanaged code debugging in the project properties settings
I need to ensure that the data transfer is either cancelled or completed for I exit.

RCW and CCW

Here is an OK intro: http://vbfaqs.blogspot.com/2008/06/overview-rcw-and-ccw.html

RCW is Runtime Callable Wripper. It is used by .NET to call COM objects.

CCW is COM callable wrapper. Used by COM to call .NET objects.

I am seeing the following error calling COM from .NET:

RaceOnRCWCleanup was detected.

An attempt has been made to free an RCW that is in use. The RCW is in use on the active thread or another thread. Attempting to free and in-use RCW can cause corruption or data loss.