我正在使用GCHandle::FromIntPtr将非托管结构指针转换为托管对象引用,参考了msdn中的示例。以下是我的代码片段:
GCHandle gch = GCHandle::FromIntPtr(IntPtr(someNativePtr));
MyManagedClass^ obj = static_cast<MyManagedClass^>(gch.Target);
我的问题是:我应该释放gch吗?
更新: 这个问题存在一个巨大的问题,就像Medinoc在他的评论中提到的那样:GCHandle::FromIntPtr不能接受指向非托管对象的IntPtr!!!所以这个问题完全没有意义。
SomeClass^
托管句柄强制转换为IntPtr,这就只能使用GCHandle类来创建一个。 - Medinoc