MonoTouch 和 IDisposable 模式

发布于 2024-12-08 22:55:58 字数 471 浏览 0 评论 0 原文

阅读 MT 文档,我发现释放内存也可以实现 IDisposable .NET 模式。

例如,在扩展 UIViewController (MyViewController) 的自定义类中,我可以重写以下方法:

public override void Dispose (bool disposing)
{
   if (disposing){
     // do some stuff here
   }
   base.Dispose (disposing)
}

从这一点开始,我的两个问题是:

  1. 除了图像之外,我还必须释放什么类型的元素?
  2. 我是否必须从 MyViewController 类的实例 (myViewController.Dispose()) 调用 Dispose 方法,或者像 dealloc 方法一样自动调用 Dispose 方法?

先感谢您。问候。

Reading MT documentation, I've seen that it is possible to release memory also implementing the IDisposable .NET pattern.

For example, in a custom class that extends UIViewController (MyViewController), I could override the following method:

public override void Dispose (bool disposing)
{
   if (disposing){
     // do some stuff here
   }
   base.Dispose (disposing)
}

Starting from this point, my two questions are:

  1. What type of elements do I have to release in addition to images?
  2. Do I have to call the Dispose method from an instance of MyViewController class (myViewController.Dispose()) or the Dispose method is called automatically like dealloc method?

Thank you in advance. Regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

酒浓于脸红 2024-12-15 22:55:58

IDisposable 的首次 MonoTouch 用法与 Mono 或 .NET 相同。您在其他地方、stackoverflowMSDN...都适用于此。

对于 MonoTouch 来说,重要的是要记住 NSObject 实现了 IDisposable ,这很有意义,因为它代表了一个本机对象。这意味着从 NSObject 继承的所有内容(monotouch.dll 的很大一部分)都实现了 IDisposable

  1. 除了图像之外,我还必须发布什么类型的元素?

大多数托管基于 NSObject 的对象实例都很小,但它们可以表示大型本机对象(GC只会知道第一个托管的大小) )。

因此,最好尽可能地处理基于 NSObject 的实例,例如,当您将它们用作局部变量时。 using 模式使得在 C# 中可以轻松做到这一点。

OTOH 根据您的判断,小的 NSString 不会占用太多内存,而其他的可能很大(或未知,例如 NSString GetWebPageContent (NSUrl)

  • 我是否必须从 MyViewController 类的实例 (myViewController.Dispose()) 调用 Dispose 方法,还是像 dealloc 方法一样自动调用 Dispose 方法?
  • Dispose 模式的一部分确保终结器 将调用 Dispose(如果之前未调用过)。因此,GC 最终将回收与这些实例关联的内存(托管和非托管/本机)。

    您可能想使用一些工具,例如 Gendarme (将在 OSX 上运行)或 FxCop (仅限 Windows),如果您的某些类型具有未正确处理的 IDisposable 字段,它将向您报告(例如)。

    免责声明:我是 Gendarme 的维护者:-)

    First MonoTouch usage of IDisposable is identical to Mono or .NET. What you read about this subject elsewhere, on stackoverflow or on MSDN... will all apply here.

    What's important wrt MonoTouch is to remember that NSObject implements IDisposable which makes a lot of sense since it represent a native object. That means that everything that inherits from NSObject, quite a large part of monotouch.dll, implements IDisposable.

    1. What type of elements do I have to release in addition to images?

    Most managed NSObject-based object instances are small but they can represent large native objects (the GC will only know about the first, managed, size).

    So it's best to Dispose NSObject-based instances when you can, e.g. when you're using them as local variables. The using pattern makes it easy to do so in C#.

    OTOH use your judgment, a small NSString won't take much memory, while others could be large (or unknown, e.g. NSString GetWebPageContent (NSUrl).

    1. Do I have to call the Dispose method from an instance of MyViewController class (myViewController.Dispose()) or the Dispose method is called automatically like dealloc method?

    Part of the Dispose pattern ensure that the finalizer will call Dispose if it has not been called previously. As such the GC will, eventually, reclaim the memory (both managed and unmanaged/native) associated with those instances.

    You might want to use some tools, like Gendarme (which will run on OSX) or FxCop (Windows-only) that will report to you (for example) if some of your types have IDisposable fields that are not disposed properly.

    Disclaimer : I'm Gendarme's maintainer :-)

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