Question

Check this code:

.. 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();
    }
}

Bitmap is ICloneable, IDisposable in C#.

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.

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.

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.

[EDIT]

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

Here is the new code:

    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
    }

and it should be proper Dispose the resources.

Was it helpful?

Solution

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.

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.

OTHER TIPS

using just calls Dispose in a finally block.
As long as you call Dispose somewhere in all code paths, you're fine.

If you don't call Dispose, the GC will eventually dispose it for you, but this can lead to resource contention.

In this particular case, you should probably dispose the original after cloning it, since it looks like you never use it again.

I can't say for sure, but if you are afraid it might, why not clone the image to a new variable, dispose the original, and then reassign:

public bool ImageCrop(int X, int Y, int W, int H)
{     
    Bitmap croppedImage = imageObject.Clone(new Rectangle(X, Y, W, H), imageObject.PixelFormat);
    imageObject.Dispose();
    imageObject = new Bitmap(croppedImage);
    croppedImage.Dispose();
}

Yes, this could be a leak.

If you are making a copy of a disposable object (in your case Bitmap), you should immediately dispose of the instance that you no longer need.

using keyword is just a convenience thing around manually having try-finally and calling Dispose(). If in your situation you can't use 'using', then simply use try-finally blocks and make sure dangling resource is cleaned up.

Memory doesn't leak in managed code, but you can cause a resource leak. The bitmap is a wrapper around a lower level object in Windows, and it's disposable so that the lower level object is cleaned up correctly. If you leave objects undisposed they should normally be disposed by the garbage collector after a while, but there is no guarantee that it will actually be disposed.

Cloning an image creates a new object that should be disposed in itself. You should dispose the original image when it's replaced by the clone. You can use the using keyword for that:

public bool ImageCrop(int X, int Y, int W, int H) {
  using (Bitmap original = imageObject) {
    imageObject = original.Clone(new Rectangle(X, Y, W, H), imageObject.PixelFormat);
  }
}

Assuming Clone() does its work properly it will give you 2 Disposable objects to manage. Both need to be Disposed().

So I don't think it will solve your problem.

It is not uncommon for a method to return an IDisposable object, you simply have to assure the (exception-safe) resource management at a higher level. Do so carefully.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top