CLI/C++将“this”转换为“this”指向整数的指针
我正在尝试在 CLI/C++ 程序中跟踪托管对象创建/处置:
::System::Diagnostics::Trace::WriteLine(String::Format(
"Created {0} #{1:X8}",
this->GetType()->Name,
((UInt64)this).ToString()));
失败并
显示错误 C2440: 'typecast' : 无法从 'MyType ^const ' 转换为 'unsigned __int64'
是否存在有没有办法以这种方式跟踪唯一的对象 ID? 谢谢!
I am trying to trace managed object creation/disposing in a CLI/C++ prog:
::System::Diagnostics::Trace::WriteLine(String::Format(
"Created {0} #{1:X8}",
this->GetType()->Name,
((UInt64)this).ToString()));
Which fails with
error C2440: 'type cast' : cannot convert from 'MyType ^const ' to 'unsigned __int64'
Is there a way to keep track of the unique object IDs this way?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,为什么这不起作用。托管句柄类型
^
不是指针。它们不仅仅是地址。托管类型的实例可以并且将会被 GC在内存中移动,因此地址不稳定;因此,为什么它不允许您执行这样的转换(因为 GC 可以随时执行,并且您不知道何时执行,任何使用此类地址作为原始值的尝试本质上都是竞争条件)。另一件经常推荐但实际上不起作用的方法是
Object.GetHashCode()
。一方面,它返回一个int
,显然不足以在 x64 上保持唯一。此外,文档不保证值是唯一的,而且它们实际上不在 2.0+ 中。唯一可行的解决方案是为您的对象创建一个 System.Runtime.InteropServices.GCHandle 实例,然后将其转换为 IntPtr - 这保证都是唯一的,并且稳定。
First of all, why this doesn't work. Managed handle types
^
aren't pointers. They aren't just addresses. An instance of a managed type can and will be moved around in memory by GC, so addresses aren't stable; hence why it wouldn't let you do such a cast (as GC can execute at any moment, and you do not know when, any attempt to use such an address as a raw value is inherently a race condition).Another thing that is often recommended, but doesn't actually work, is
Object.GetHashCode()
. For one thing, it returns anint
, obviously not enough to be unique on x64. Furthermore, the documentation doesn't guarantee that values are unique, and they actually aren't in 2.0+.The only working solution is to create a an instance of
System.Runtime.InteropServices.GCHandle
for your object, and then cast it toIntPtr
- that is guaranteed to be both unique, and stable.查看
GCHandle
类型:http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.gchandle.aspx。看起来它会做你想做的事,尽管看起来它对于你的目的来说会有点痛苦......Check out the
GCHandle
type: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.gchandle.aspx. Looks like it would do what you want, though it looks it would be a bit of a pain to use for your purposes...即使您可以将
this
转换为某个整数值以进行显示,它也可能不是一个有用的唯一标识符。这是因为与 C++ 不同,在 C++/CLI 中,(托管)对象的位置(以及扩展后的值)可能会在该对象的生命周期内发生变化。 (逻辑上)相同的对象可以在程序中的不同点打印两个不同的字符串。Even if you could cast
this
to some integral value for display, it probably wouldn't be a useful unique identifier. This is because unlike C++, in C++/CLI the location of a (managed) object (and by extension the value of this) can potentially change during that object's lifetime. The (logically) same object could print two different strings at different points in the program.MyType ^const 是引用类型。因此,它位于托管内存空间中,并且您无法获取指向这些类型的直接内存指针,因为它们可以随时更改。
您可以使用 MyType.GetHashCode();
MyType ^const is a reference type. Hence it's in the managed memory space, and you can't get direct memory pointers to these types, as they can change at any time.
You could use MyType.GetHashCode();