You can use auto_gcroot<T>
, instead of gcroot<T>
. The difference is that auto_gcroot<T>
's destructor "also destructs the owned object."
In the managed world, this maps through to calling IDisposable.Dispose()
on the wrapped managed type, provided it implements IDisposable
.