user3830473
user3830473

Reputation: 13

C# Class De-initialization of Dynamic Memory

I am using a class that dynamically allocates an array during construction, as follows:

    class HeightMap
{
    private int width;
    private int height;
    private ulong numPixels;
    private float[,] value;

    public HeightMap(int width, int height)
    {
        if (width <= 0) throw new Exception("Invalid Dimension: Width");
        if (height <= 0) throw new Exception("Invalid Dimension: Height");

        // Make sure there is an uneven width and height (for allowing diamond square algorithm)
        if ((width % 2) == 0) width++;
        if ((height % 2) == 0) height++;

        this.numPixels = (ulong)(width * height);
        this.width = width;
        this.height = height;
        this.value = new float[width, height];
    }


}

Am I required to implement IDisposable to release the value array on destruction, or is this automatically handled when the object is invalidated? [Anything else possibly wrong with this method of allocation in the constructor?]

Upvotes: 1

Views: 126

Answers (2)

Rangesh
Rangesh

Reputation: 728

You need not Use IDisposable.They are generally used with Unmanaged Code.The Garbage Collector would take care of your Object Disposal.

Upvotes: 1

David Crowell
David Crowell

Reputation: 3813

You're allocating the memory through managed code. You don't need to worry about it.

Upvotes: 2

Related Questions