C++/CLI: preventing garbage collection on managed wrapper of unmanaged resource

前端 未结 2 1918
忘了有多久
忘了有多久 2021-01-11 17:14

I have a C++ unmanaged class NativeDog that needs to be used from C#, so I\'ve create a wrapper class ManagedDog.

// unmanaged C++          


        
相关标签:
2条回答
  • 2021-01-11 17:42

    In your managed code, add GC.KeepAlive(dog) following the call to FeedDogManaged():

    http://msdn.microsoft.com/en-us/library/system.gc.keepalive(VS.71).aspx

    0 讨论(0)
  • 2021-01-11 17:44

    You need a GC::KeepAlive() call in your FeedDogManaged function. Seems like it is an exact use case for that.

    0 讨论(0)
提交回复
热议问题