Why didn’t we use RoRegisterForApartmentShutdown to clean up the unmarshalable delegate?
cppdashboard.dev/r/2026/10/why-didn-t-we-use-ro-register-for-apartment-shutdown-to-cleaThis article discusses the challenges of using the RoRegisterForApartmentShutdown function in creating an agile wrapper for non-agile objects in COM. It highlights the complexities involved in managing object lifetimes and apartment shutdowns.
From the article
In my attempt to create a fake agile wrapper that is technically agile but is not useful outside its home apartment , Luca Bacci suggested, among other things , using the RoRegisterForApartmentShutdown function.
The RoRegisterForApartmentShutdown function is nice, but it doesn’t suit our needs.
One problem is that the object registered for apartment shutdown must be agile. (If you pass a non-agile object, the call fails.) That means that our wrapper must be agile. If we put the non-marshalable non-agile object inside our wrapper, we now have an agile object (the wrapper) that holds a non-agile object (the original object) as a member, which creates the risk that the wrapper will run down in the wrong apartment. The normal way of fixing that is to use RoGetAgileReference to…
Share this resource