Article

Why didn’t we use Ro­Register­For­Apartment­Shutdown to clean up the unmarshalable delegate?

The Old New Thing ·The Old New Thing ·Published 2026-10-21 ·5 min read

This article discusses the challenges of using the Ro­Register­For­Apartment­Shutdown 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 Ro­Register­For­Apartment­Shutdown function.

The Ro­Register­For­Apartment­Shutdown 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 Ro­Get­Agile­Reference to…


Share this resource


Discovered 2026-10-22 Source The Old New Thing Archive 2026-10 →