Monotouch 中的 dealloc 等价物是什么?

发布于 2024-09-29 07:54:11 字数 455 浏览 0 评论 0原文

我正在网上查看一个示例,其中包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

银河中√捞星星 2024-10-06 07:54:11

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.

枫林﹌晚霞¤ 2024-10-06 07:54:11

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.

欢你一世 2024-10-06 07:54:11

您不需要调用release或dealloc,它们由MonoTouch处理。

You don't need to call release or dealloc, they're taken care of by MonoTouch.

浅唱ヾ落雨殇 2024-10-06 07:54:11

来自 Xamarin 文档

http://docs.xamarin.com/ios/advanced_topics/api_design#When_to_call_Dispose< /a>

当您需要 Mono 来清除对象时,您应该调用 Dispose。一个可能的用例是,Mono 不知道您的 NSObject 实际上持有对重要资源(如内存或信息池)的引用。在这些情况下,您应该调用 Dispose 立即释放对内存的引用,而不是等待 Mono 执行垃圾收集周期。
在内部,当 Mono 从 C# 字符串创建 NSString 引用时,它会立即处理它们,以减少垃圾收集器必须完成的工作量。需要处理的对象越少,GC 运行的速度就越快。”

From Xamarin Documentation

http://docs.xamarin.com/ios/advanced_topics/api_design#When_to_call_Dispose

You should call Dispose when you need Mono in getting rid of your object. A possible use case is when Mono has no knowledge that your NSObject is actually holding a reference to an important resource like memory, or an information pool. In those cases, you should call Dispose to immediately release the reference to the memory, instead of waiting for Mono to perform a garbage collection cycle.
Internally, when Mono creates NSString references from C# strings, it will dispose them immediately to reduce the amount of work that the garbage collector has to do. The fewer objects around to deal with, the faster the GC will run."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文