Monotouch 中的 dealloc 等价物是什么?
我正在网上查看一个示例,其中包含 Objective-c 中的此代码,
-(void)dealloc {
[activeController viewWillDisappear:NO];
[activeController.view removeFromSuperview];
[activeController viewDidDisappear:NO];
[activeController release];
[super dealloc];
}
我假设 MT 等效项是 Dispose,我是否正确?
我不需要调用:
[activeController release];
[super dealloc];
方法,因为它们将在 Monotouch 上被垃圾收集,这也正确吗?
I am looking at an example online that contains this code in objective-c
-(void)dealloc {
[activeController viewWillDisappear:NO];
[activeController.view removeFromSuperview];
[activeController viewDidDisappear:NO];
[activeController release];
[super dealloc];
}
I assume the MT equivalent would be Dispose, am I correct?
I won't need to call the:
[activeController release];
[super dealloc];
methods as they will be Garbage collected on Monotouch, is this also correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
MonoTouch 是垃圾收集器,因此您无需担心自己进行释放。
话虽这么说,有时您意识到内存中保留了一些大量资源,并且您希望通过立即处理这些资源来协助系统,而不是等待垃圾收集器启动。
这就是调用 Dispose 的时候方便:它在垃圾收集器之前释放关联的资源。这对于图像等大型对象尤其重要,因为图像存储在非托管堆上,而对象引用存储在托管堆中。
这意味着,如果您有一个 8 MB 的映像:8 MB 存储在非托管堆(由 Objective-C 管理)中,1 个指针(4 个字节)存储在托管堆中。就 Mono 的垃圾收集器而言,您使用的是 4 个字节,而不是 8 个兆。
因此,在这种情况下,您可以通过调用 dispose 来协助系统:您知道看似无辜的“myImage”变量实际上指向一大块内存。
MonoTouch is garbage collected, so you do not need to worry about doing the deallocation yourself.
That being said, there are times when you are aware that you are keeping some large resources in memory and you want to assist the system by disposing the resources right away instead of waiting for the garbage collector to kick in.
This is when calling Dispose comes in handy: it releases the resources associated before the garbage collector has to. This is particularly important for large objects, like images, as images are stored on the unmanaged heap, while object references are stored in the managed heap.
What this means is that if you have a 8 megabyte image: 8 megabytes are stored in the unmanaged heap (managed by Objective-C) and 1 pointer (4 bytes) in the managed heap. As far as Mono's Garbage Collector is concerned, you are using 4 bytes, not 8 megs.
So it is times like this when you can assist the system by calling dispose: you know that the innocently looking "myImage" variable actually points to a large blob of memory.
Monotouch 被垃圾收集。在对象被垃圾收集之前,会调用该对象的析构函数。
这是Microsoft 关于 C# 析构函数的页面。我不知道Monotouch中是否有更多关于析构函数的相关文档。
Monotouch is garbage collected. Before an object is garbage collected, the destructor for the object is called.
Here's Microsoft's page about C# destructors. I don't know if there's more relevant documentation for destructors in Monotouch.
您不需要调用release或dealloc,它们由MonoTouch处理。
You don't need to call release or dealloc, they're taken care of by MonoTouch.
来自 Xamarin 文档
http://docs.xamarin.com/ios/advanced_topics/api_design#When_to_call_Dispose< /a>
From Xamarin Documentation
http://docs.xamarin.com/ios/advanced_topics/api_design#When_to_call_Dispose