InvalidMemoryOperationError@(0)

Andre via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Nov 17 07:41:25 PST 2014


Hi,

the following coding is an extract from the DGUI library.
On my system (Win 8) I receive InvalidMemoryOperationError at .
I got the information that this Error doesn't not occur on
a WinXP system.

I think there is definetely a bug in the following coding.

The Grid class has a Collection of Rows. Each Row has a Collection
of Cols.
The class Row has a destructor which has an empty foreach over
the Collection of Cols. If I comment this empty foreach no
error is thrown. I assume the class Collection accesses a Col
object which is already destructed?

Does DMD behave correctly (InvalidMemoryOperationError) and
can where in the Collection class exactly the invalid object is
accessed? I tried to find the invalid coding line, but without 
success.

class Collection(T)
{
	private T[] _t;

	int add(T t)
	{
		this._t ~= t;
		return this._t.length - 1;
	}

	void clear()
	{
		this._t.length = 0;
	}

	T[] get()
	{
		return this._t;
	}

	@property int length()
	{
		return this._t.length;
	}

	T opIndex(int i) nothrow
	{
		if(i >= 0 && i < this._t.length)
		{
			return this._t[i];
		}
		assert(false, "Index out of range");
	}

	int opApply(int delegate(ref T) dg)
	{
		int res = 0;

		if(this._t.length)
		{
			for(int i = 0; i < this._t.length; i++)
			{
				res = dg(this._t[i]);

				if(res)
				{
					break;
				}
			}
		}
		return res;
	}
}

class Col {}

class Row
{
	private Collection!(Col) _columns;
	
	this()
	{
		this._columns = new Collection!(Col)();
	}
	
	~this()
	{
		foreach(cp; this._columns)
		{	
		}
	}
	
	
	Col addColumn()
	{
		Col cp = new Col();
		this._columns.add(cp);
		return cp;
	}
}

class Grid
{
	private Collection!(Row) _rows;
	
	this()
	{
		this._rows = new Collection!(Row)();
	}
	
	@property public Row[] rows()
	{
		return this._rows.get();
	}
	
	Row addRow()
	{	
		Row rp = new Row();
		this._rows.add(rp);
		return rp;
	}
	
	void clear()
	{
		_rows.clear();
	}
}

void main()
{
	auto grid = new Grid();

	for(int i=0; i< 100000; i++)
	{
		grid.clear();

		with(grid.addRow())
		{
			addColumn();
			addColumn();
			addColumn();
		}
	}
}


More information about the Digitalmars-d-learn mailing list