COM调用来自Go的数据由垃圾收集器(GC)收集,并清零已使用的内存。

huangapple go评论76阅读模式
英文:

COM call from go has its data collected by GC, zeroing used memory

问题

我有一个Go程序,执行一个WMI查询,然后将数据转换回Go语言的数据结构(使用这里的方法)。每隔一段时间,Go的垃圾回收机制会将一些看似随机的内存部分清零,导致严重的故障。

我正在尝试弄清楚到底是什么导致了这个问题,我相信下一步是了解在COM调用期间发生了什么。我目前的理解是:

  1. 从进程中使用WMI查询调用COM
  2. 操作系统执行查询并将结果写入由该进程拥有的某个内存位置
  3. 该位置从COM调用返回,然后我可以访问和序列化它

这大致是发生的情况吗?Windows如何选择内存位置,以避免覆盖现有数据?

英文:

I have a go program that executes a WMI query then converts the data back into go-land data structures (using the method here). Every so often, go's GC comes along and nukes some seemingly random parts of memory to 0s, causing horrible breakage.

I am trying to figure out what exactly is causing this problem, and the next step, I believe, is to understand what happens during the COM call. My current understanding is:

  1. call into COM with the WMI query from a process
  2. the OS executes the query and writes the results into some memory location owned by the process
  3. that location is returned from the COM call, which I can then access and serialize

Is this about what happens? How does Windows choose that memory location such that it doesn't overwrite existing data?

答案1

得分: 1

每个COM对象都使用AddRef()和Release()进行引用计数。也许你需要额外的AddRef()来延长其生命周期。

从示例代码中我看到有很多延迟的Release调用。这是可以的,因为我们希望在main函数结束时释放这些对象。在你的程序中,你可能希望更长时间地延迟Release(但是,我不了解Go语言或者defer的具体作用)。

英文:

Every COM object is reference counted with AddRef() and Release(). Perhaps you need an extra AddRef() to keep it around longer.

I see from the sample code that there are a lot of defered Release calls. This is fine because we want the objects released at the end of main. In your program you might want to put off Release even longer (but, I don't know go or exactly what defer does)

huangapple
  • 本文由 发表于 2014年2月27日 10:18:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/22057755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定