资料 - 单 - 其他根逆参考
当设置一Monotouch.Dialog实例:资料 - 单 - 其他根逆参考
- 定制
UIBubbleMapElement
元件由GC设置; - 对于每个处置元素,自定义
UIBubbleMapCell
也被GC处置; - 但是对于所有处理过的单元,它们的自定义
UIBubbleMapView
都没有处理。
要解决此问题,我开始使用Mono Profiler应用程序。
的问题是:寻找在不设置UIBubbleMapView
实例逆引用图像。我怎么能释放这最后的参考,并允许收集我的自定义视图?
最后,这是我UIBubbleMapCell
Dispose方法:
protected override void Dispose (bool disposing) {
bubbleMapView = null;
System.Diagnostics.Debug.WriteLine ("############# {0} 'Dispose' {1}.", this, disposing ? "invoked directly" : "called by the garbage collector finalizer");
base.Dispose (disposing);
}
这是我得到打印到控制台:
############# <UIBubblesViewController: 0x152427c0> 'Dispose' called by the garbage collector finalizer.
############# <UIBubbleMapCell: 0x152b6a40; baseClass = UITableViewCell; frame = (0 195; 320 38); autoresize = W; layer = <CALayer: 0x152c65c0>> 'Dispose' called by the garbage collector finalizer.
############# <UIBubbleMapCell: 0x1524aba0; baseClass = UITableViewCell; frame = (0 35; 320 38); autoresize = W; layer = <CALayer: 0x152038f0>> 'Dispose' called by the garbage collector finalizer.
############# <UIBubbleMapCell: 0x17c91710; baseClass = UITableViewCell; frame = (0 233; 320 116); autoresize = W; layer = <CALayer: 0x152cbb80>> 'Dispose' called by the garbage collector finalizer.
############# <UIBubbleMapCell: 0x1520b2c0; baseClass = UITableViewCell; frame = (0 108; 320 52); autoresize = W; layer = <CALayer: 0x17c2fc30>> 'Dispose' called by the garbage collector finalizer.
编辑:罗尔夫感谢您的回答。
首先,我添加的下一个代码到的UITableViewCell Dispose方法:
bubbleMapView.Dispose();
bubbleMapView = null;
虽然接收控制台内部的下一个消息,单声道分析器仍然呈现所述对象作为未收集。与以前相同的图像。
############# <UIBubbleMapView: 0x154af370; frame = (0 0; 1 1); layer = <CALayer: 0x154af0e0>> 'Dispose' invoked directly.
在API中运行时,我可以看到它的引用计数大于1。
在图像有一个UIBubbleTextView
实例,但是它在相同的方式UIBubbleMapView
情况下的行为完全。我的UIBubbleMapView
拥有一些其他意见。这是反向引用未检查时的分析器信息。是否有一些技巧来处理这些子视图?
的<Other Root>
通常是一个的GCHandle Xamarin.iOS内部使用,以保持管理对象的生命,直到相应的原生对象被释放。打破这个链接的一种方法是在对象上调用Dispose(你没有提到在UIBubbleMapView上调用Dispose),在这种情况下,托管对象将被GC收集(除非它被其他托管代码引用)。
很可能有一些其他本地代码持有对此UIBubbleMapView实例的引用,但是要准确找出发生了什么,您需要使用乐器中的分配工具进行配置文件(启用参考计数跟踪来精确跟踪哪些代码保留该物体)。
更新
直到你调用Dispose被管理对象,管理对象将保留原生对象[1]。这意味着如果保留计数大于1,那么该对象还有额外的本地保留(这也意味着一旦您在对象上调用Dispose,其余的引用都是本地的)。请注意,此时托管对象可能由GC收集,因此您不能使用HeapShot跟踪(本机)对象,您必须使用Instruments。
提示:如果您在Instruments中启用了右侧边栏,则会得到每个保留/释放调用的堆栈跟踪。这对追踪谁保留对象非常有用。
[1] Xamarin.iOS也将释放被管理对象一旦保留计数达到1时的引用(并且GC已确定管理对象未从其他托管代码引用)。