开发者

storing reference to managed class in native class and updating it in managed module

I know I can allocate and store 开发者_JAVA技巧a handle to a managed class in a native class in C++/CLI project using gcroot template. The questions are: 1.How can I pass the reference to a ManagedClass that I reference with gcroot to another managed class in managed module? 2.Can anything go wrong when I update the ManagedClass in managed module? For example can garbage collection move it in memory so that the handle wrapped by gcroot will no longer be valid?


  1. How can I pass the reference to a ManagedClass that I reference with gcroot to another managed class in managed module?

Just pass the gcroot. gcroot has an operator conversion to the contained T^.

  1. Can anything go wrong when I update the ManagedClass in managed module?

Nothing concerning comes to mind.

For example can garbage collection move it in memory so that the handle wrapped by gcroot will no longer be valid?

No. The gcroot wraps the "handle"--its not a direct pointer, so if the garbage collector moves the pointed to object, the handle (held on to by the gcroot) will be updated to know the new location. Thus, the gcroot will still be valid.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜