我需要在我的对象中实现处置或终结吗?

发布于 2024-09-15 00:45:56 字数 623 浏览 9 评论 0原文

长期以来,我让垃圾收集器发挥其魔力,消除了我自己的所有责任。

遗憾的是,它从未成为一个问题……所以我从未再考虑过这个问题。

现在,当我思考它时,我真的不明白“dispose”函数的真正作用是什么以及应该如何以及何时实现它。

完成同样的问题......

最后一个问题...... 我有一个类 pictureManipulation :当我需要保存/调整大小/更改格式时...我启动该类的一个新实例,使用它的对象并且...让垃圾收集杀死该实例

class student
{
   public void displayStudentPic()
   {
      PictureManipulation pm = new PictureManipulation();
      this.studentPic = pm.loadStudentImage(id); 
   }
}

Class Test
{
  student a = new Student();
  a.displayStudentPic();
  // Now the function execution is ended... does the pm object is dead? Will the GC will kill it?
}

For too long I let the garbage collector do its magic, removing all responsibilities from my self.

Sadly it never turned into an issue... So I never gave a second thought to the subject.

Now when I think about it I don't really understand what the "dispose" function really does and how and when it should be implemented.

The same question for finalize...

And a last question...
I have a class pictureManipulation : when I need to save/resize/change format ... I start a new instance of that class use its objects and... well let the garbage collection kill the instance

class student
{
   public void displayStudentPic()
   {
      PictureManipulation pm = new PictureManipulation();
      this.studentPic = pm.loadStudentImage(id); 
   }
}

Class Test
{
  student a = new Student();
  a.displayStudentPic();
  // Now the function execution is ended... does the pm object is dead? Will the GC will kill it?
}

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

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

发布评论

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

评论(3

滥情空心 2024-09-22 00:46:11

如果对象持有的资源不仅仅是对象本身持有的内存,那么您需要注意对象的处置。

例如,如果你的对象抽象了一个文件,那么当文件被释放时你必须控制,否则,你会把事情搞砸得很糟糕:你的应用程序使用完它,它仍然会被锁定,直到 GC 处理你的对象。

要了解如何正确执行此操作,请阅读有关 dispose 和 Finalize 以及 using(){} 子句的手册。

You need to take care about object disposal if it holds resources other than just memory held by the object itself.

For instance, if your object abstracts a file, you must be in control when the file is released, or, you will mess things up very bad: your app finished using it and it will still be locked, until GC disposes your object.

To know how to do it properly, read manuals about dispose and finalize as well as the using(){} clause.

躲猫猫 2024-09-22 00:46:09

仅当您的类型拥有一些非托管资源(例如数据库连接、文件句柄等)或者您的类型拥有的某些对象实现了 IDisposable 接口时,您才需要实现 Dispose 方法。在实现标准 Dispose 模式时,您应该考虑以下几点:

  • 如果您的对象不包含任何 IDisposable 对象或非托管资源(例如数据库连接),那么您根本不需要实现 IDisposable 或终结器,
  • 如果您的对象保存对 IDisposable 对象的引用,然后在 Dispose 方法中对这些对象调用 Dispose()
  • 如果您的对象不保存任何非托管资源,则不要实现终结器,垃圾收集器不会尝试终结您的对象(这会影响性能)除非您已经实现了终结器。
  • 如果您的对象拥有非托管资源,请在终结器中清理它们,而无需在 Dispose(bool) 方法中重写任何清理代码。

You only need to implement the Dispose method if your type holds some unmanaged resources like DB connections, file handles, etc. or if some of the objects that are held by your type implement the IDisposable interface. Here is a few points you should consider when implementing the standard Dispose pattern:

  • if your object doesn’t hold any IDisposable objects or unmanaged resources (DB connection, for example) then you don’t need to implement the IDisposable or finalizer at all
  • if your object holds references to IDisposable objects, then call Dispose() on these objects in the Dispose method
  • if your object doesn’t hold any unmanaged resources then don’t implement a finalizer, the Garbage Collector won’t attempt to finalize your object (which has a performance hit) unless you have implemented a finalizer.
  • if your object holds unmanaged resources, clean them up in the finalizer without re-writing any of the cleanup code in the Dispose(bool) method already.
朱染 2024-09-22 00:46:07

关于您的班级学生

我需要 Dispose() 吗?

假设 Picture 类是 IDisposable:。因为 Student 对象“拥有”studentPic,这使得它负责清理它。一个最小的实现:

class Student : IDisposable
{
   private PictureClass studentPic;
   public void Dispose()
   {
      if (studentPic != null)
        studentPic.Dispose();
   }
   ...
}

现在您使用一个 Student 对象,例如:

void Test
{
  using (Student a = new Student())
  {
     a.displayStudentPic();    
  } // auto Dispose by using() 
}

如果您不能/不使用 using(){} 块,只需调用 a.Dispose();< /code> 当你完成后。

但请注意,这里(远)更好的设计是避免将图片对象保留在 Student 对象中。这就引发了整个责任链。

我需要终结器吗?

。因为当收集 Student 对象时,保证在同一运行中收集其 StudentPic 对象。终结器(析构函数)毫无意义,但仍然昂贵。

Regarding your class Student

Do I need a Dispose() ?

Assuming the Picture class is IDisposable: Yes. Because a Student object 'owns' the studentPic and that makes it responsible for cleaning it up. A minimal implementation:

class Student : IDisposable
{
   private PictureClass studentPic;
   public void Dispose()
   {
      if (studentPic != null)
        studentPic.Dispose();
   }
   ...
}

And you now use a Student object like:

void Test
{
  using (Student a = new Student())
  {
     a.displayStudentPic();    
  } // auto Dispose by using() 
}

If you can't/don't use a using(){} block, simply call a.Dispose(); when you're done with it.

But please note that the (far) better design here would be to avoid keeping a picture object inside your Student object. That sets off a whole chain of responsibilities.

Do I need a Finalizer?

No. Because when a Student object is being collected, its studentPic object is guaranteed to be collected in the same run. A Finalizer (destructor) would be pointless but still expensive.

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