- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 Go 中实现一个弱引用,以便我可以使用终结器来检测何时不再需要数据结构并能够存储/清理数据。
我发现的一种方法是使用 uintptr 作为映射键,这样当调用终结器时,我可以使用传递给终结器函数的指针值访问/清理数据。这样做安全吗?
我想我的问题是:Go 使用移动垃圾收集器吗?还是会?
最佳答案
The link你自己引用的in a comment提供您要问的问题的答案:不要给客户一个指向您想要大惊小怪的底层对象的指针,而是给他们一个指向包装器对象的指针:
type internal struct {
// all the internal stuff goes here
}
// change the name Wrapper below to something more suitable
type Wrapper struct {
*internal // or p *internal if you want to be overly verbose
}
func NewWhatever(/*args*/) *Wrapper {
p := &Wrapper{...} // fill this part in
runtime.SetFinalizer(p, wrapperGotCollected)
return p
}
func wrapperGotCollected(p *Wrapper) {
// since p itself is about to be collected,
// **p (or *((*p).p)) is no longer accessible by
// the user who called NewWhatever(). Do
// something appropriate here.
}
请注意,这不在*internal
上使用终结器,而是在*Wrapper
上使用终结器:当时wrapperGotCollected
被调用,*internal
对象本身保证仍然存在,因为 p
本身还没有被 GC-ed(有点像到一半,并且将在 wrapperGotCollected
返回后不久或之后完成剩余的路程。
关于go - 使用 uintptr 作为弱引用是否安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63025066/
在Go的runtime/proc.go中,有一段代码如下所示: // funcPC returns the entry PC of the function f. // It assumes that
我想在 Go 中实现一个弱引用,以便我可以使用终结器来检测何时不再需要数据结构并能够存储/清理数据。 我发现的一种方法是使用 uintptr 作为映射键,这样当调用终结器时,我可以使用传递给终结器函数
这应该很简单:我到处都看到人们使用 IntPtr,我有什么理由应该改用 UIntPtr 吗? 最佳答案 似乎没有充分的理由这样做。来自文档: Remarks ... The IntPtr type i
我只是找不到获取指向给定结构的每个属性的指针片段的方法。我正在使用反射来获取我的指示(感谢 https://stackoverflow.com/a/24348352/6093604 ) if valu
我正在尝试将系统调用与 user32.dll 结合使用以获取剪贴板的内容。我希望它是来自打印屏幕的图像数据。 现在我得到了这个: if opened := openClipboard(0); !ope
我需要将字符串作为参数传递给 golang 中的 C DLL,我想得到这样的东西: proc, e = syscall.GetProcAddress(h, "JLINKARM_ExecComman
我正在查看 RegOpenKeyEx 的 P/Invoke 声明当我注意到页面上的这条评论时: Changed IntPtr to UIntPtr: When invoking with IntPtr
我试过这样转换它: UIntPtr x = (UIntPtr)intPtr; ...但是编译器对此不太满意并返回编译错误。 我需要进行转换,因为 RegOpenKeyEx 的 P/Invoke 签名需
在下面的代码中,Resharper 给我一个警告:Cannot cast expression of type 'Color' to type 'UIntPtr'。 (实际上,Resharper 认为
我在 golang 网站上读到 uintptr 存储指针值的未解释位,我在网上找到的那种答案非常困惑。有人可以用简单的词向我解释一下吗。 最佳答案 简答:当您需要像使用数字一样使用地址时使用它。 Go
根据文档(https://golang.org/pkg/unsafe/#Sizeof)unsafe.Sizeof返回给定表达式的大小(以字节为单位)。理想情况下,任何给定表达式的大小都可以用 uint
给定 // I know that behind SomeInterface can hide either int or a pointer to struct // In the real cod
我尝试使用来自 golang 包的系统调用调用 WindowsAPI(尽量不要使用 cgo),但我面对 uintptr 我不知道如何访问 uintptr 具有其地址的数据 这是我正在做的代码 pack
Go 编程语言在第 13.2 节中说这是代码是安全的并且 x 将始终对垃圾收集器可见: pb := (*int16)(unsafe.Pointer( uintptr(unsafe.Pointer(
根据Golang tour ,我们提供了以下整数类型: int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr 理
我需要在我的 C# .NET 2.0 应用程序中将 UIntPtr 对象转换为 IntPtr 对象。如何做到这一点?我不认为它像这样简单: UIntPtr _myUIntPtr = /* Some i
我不知道怎么办。它不使用 []byte(uintptr(0)) 进行编译。 请给我举个例子。 最佳答案 这取决于您机器的架构(32 位或 64 位)以及您希望如何对其进行编码(小端或大端)。无论哪种方
我正在尝试将在 uintptr 类型的变量中找到的值分配给 Go 中的 uint64 变量。使用 myVar = valFromSystem 给我 cannot use valFromSystem (
这是一个看起来很简单的问题: 鉴于 native 大小的整数最适合算术,为什么 C#(或任何其他 .NET 语言)不支持使用 native 大小的算术 IntPtr和 UIntPtr ? 理想情况下,
我是一名优秀的程序员,十分优秀!