Managing destructors of managed (C#) and unmanaged (C++) objects

后端 未结 4 942
轻奢々
轻奢々 2021-01-14 02:46

I have a managed object in a c# dll that maintains an anonymous integer handle to an unmanaged object in a c++ dll. Inside the c++ dll, the anonymous integer is used in an s

4条回答
  •  -上瘾入骨i
    2021-01-14 03:02

    The usual way to do this is to derive your managed object from IDisposable

    I always try to call object.Dispose explicitly when I'm done with the object, but I'm not sure that would be necessary in your case. The documentation that I've read is unclear as to whether it gurantees that Dispose() will be called before your dll unloads or not.

    In my own code, the managed code domain is torn down explicitly before the unmanaged app exits so I don't have to worry about that particular problem.

提交回复
热议问题