一次性对象克隆会导致C#中的内存泄漏吗? [英] Will disposable object clone cause memory leak in C#?

查看:77
本文介绍了一次性对象克隆会导致C#中的内存泄漏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查此代码:

.. class someclass : IDisposable{
    private Bitmap imageObject;
    public void ImageCrop(int X, int Y, int W, int H)
    {
        imageObject = imageObject.Clone(new Rectangle(X, Y, W, H), imageObject.PixelFormat);
    }
    public void Dispose()
    {
        imageObject.Dispose();
    }
}

位图在C#中是 ICloneable IDisposable .

为了避免内存泄漏,对于一次性对象,通常使用 using ,那么无论您的代码有多错误,该对象都会由系统自动处理.

Too avoid memory leak, for Disposable object, normally use using, then the object will be disposed by system automatically no matter how wrong your code went.

在我的示例中,由于我不想处置对象,因此无法使用 using ,以后我需要它(整个类将自行处置,因为其 IDisposable 为好吧.

In my sample, I cannot use using since I do not want to dispose the object, I need it later (the whole class will dispose itself since its IDisposable as well.

我的问题是:我有一个 imageObject 对象,然后使用它 Clone()方法克隆一个新对象并将其提供给旧的对象变量.这会导致一个对象(克隆的对象还是原始的对象)无处可去,永远也不会被处置,从而导致内存泄漏.

My question is: I have a imageObject object, then I use it Clone() method clone a new object and give it to the old object variable. Will this cause one (either the cloned or the original) object go nowhere and never get disposed, a memory leak.

似乎大多数意见是 Clone 导致其他对象,旧意见应该是 Dispose()

Seems most opinions are Clone cause additional object, the old one should be Dispose()

这是新代码:

    public void ImageCrop(int X, int Y, int W, int H)
    {
            // We have 1 object: imageObject
            using (Bitmap croppedImage = imageObject.Clone(new Rectangle(X, Y, W, H), imageObject.PixelFormat))
            {
                    // We have 2 objects: imageObject and croppedImage
                    imageObject.Dispose(); // kill one, only croppedImage left
                    imageObject = new Bitmap(croppedImage); // create one, now 2 objects again
            } // the using() will kill the croppedImage after this
            // We have 1 object: imageObject
    }

应该适当地处置资源.

推荐答案

避免资源泄漏或过早处置错误的关键是确保每个IDisposable对象在任何时候都将始终是一个明确定义的负责处置的所有者.它.有时,对象会公开一个方法,通过该方法它可以假定传入对象的所有权.如果对象的所有者将其传递给这种方法,则对象的原始所有者不应对其进行处置.否则,对象的所有者必须先销毁对象,然后再销毁对它的最后引用.

The key to avoiding resource leaks or premature-disposal bugs is to ensure that every IDisposable object will at all times exactly one clearly-defined owner who is responsible for disposing it. Sometimes an object will expose a method by which it will assume ownership of a passed-in object. If the owner of an object passes it to such a method, the original owner of the object should not dispose it. Otherwise, the owner of an object must dispose of an object before destroying its last reference to it.

如果 someClass 拥有 ImageObject ,那么它可能应该在销毁对该对象的引用之前将其处置.另一方面,如果一个对象持有对另一个对象的唯一引用,则出于重新分配原始引用的目的而克隆所持有的对象似乎有点代码味道.我不知道ImageObject最初是如何分配的,但似乎应该在您的对象内创建它,或者根据传入的图像对象克隆它.无论哪种情况,您都应该能够对传入图像的类型进行足够的控制,以选择一种可以裁剪而不必(重新)克隆的图像.

If someClass owns ImageObject, then it should probably dispose that object before destroying a reference to it. On the other hand, if an object holds the only reference to another object, cloning the held object for the purpose of reassigning the original reference seems like a bit of a code smell. I don't know how ImageObject gets assigned initially, but it would seem that it should either be created within your object, or cloned based upon a passed-in image object. In either case, you should be able to exercise enough control over the type of the passed-in image to choose a type which can be cropped without having to be (re)cloned.

这篇关于一次性对象克隆会导致C#中的内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆