[Mono-list] Memory leak

Alan McGovern alan.mcgovern at gmail.com
Sat Feb 7 08:08:57 EST 2009


Hi,

Could you point out the mistake i made in porting your example to C#,
because it leaks forever on MS.NET too.

Thanks,
Alan.


using System;

class Leak
{
    class Cell<A>
    {
        public A content;
        public Cell<A> next;
    }

    static Cell<int[]> tail = null;

    static void Push(int[] x)
    {
        var tail2 = tail;
        var cell = new Cell<int[]> { content = x, next = tail2.next };
        tail2.next = cell;
        tail = cell;
    }

    static void Pop()
    {
        var tail3 = tail;
        tail3.next = tail3.next.next;
    }

    static void Main()
    {
        var cell = new Cell<int[]> { content = new int[0], next = null };
        cell.next = cell;
        tail = cell;

        while (true)
        {
            Push(new int[] { 1, 2, 3, 4 });
            Pop();
        }
    }
}


More information about the Mono-list mailing list