Hi!
I did not think it was possible, but heck
Here is the complete
source that gives reproducable error at least on my machine.
After you run it, you should get an exception Length = 0 indicating
that the problem is present..
public class Vec1
{
public int Length;
public Vec1()
{
Length = 100;
}
}
public class Vector1
{
internal Vec1 Data;
static private int VecIterCount = 0;
static private int GCInterval = 200;
static private int GCGeneration = 0;
protected virtual void Dispose(bool disposing)
{
if (Data != null)
{
Data.Length = 0;
}
}
~Vector1() //finalizer
{
Dispose(false);
}
public void CreateFromCache()
{
if (Data == null)
{
Data = new Vec1();
VecIterCount++;
if ((VecIterCount % GCInterval) == 0)
{
GC.Collect(GCGeneration);
VecIterCount = (VecIterCount % GCInterval) + 1;
}
}
}
public Vector1(int aLength)//: base(IntPtr.Zero, true)
{
Data = null;
CreateFromCache();
Data.Length = aLength;
}
public Vector1(Vec1 Src)//: base(IntPtr.Zero, true)
{
Data = null;
CreateFromCache();
Data.Length = Src.Length;
}
public static implicit operator Vec1 (Vector1 AValue)
{
return AValue.Data;
}
public int Length
{
get
{
return this.Data.Length;
}
set
{
this.Data.Length = value;
}
}
public static Vector1 operator *(Vector1 Left, double Right)
{
Vector1 vector = new Vector1(0);
vector.Length = Left.Length;
return vector;
}
public static Vector1 operator *(double Left, Vector1 Right)
{
Vector1 vector = new Vector1(0);
vector.Length = Right.Length;
return vector;
}
public static Vector1 operator *(Vector1 Left, Vec1 Right)
{
Vector1 vector = new Vector1(0);
vector.Length = Left.Length;
return vector;
}
public static Vector1 operator *(Vec1 Left, Vector1 Right)
{
Vector1 vector = new Vector1(0);
vector.Length = Right.Length;
return vector;
}
public static Vector1 operator *(Vector1 Left, Vector1 Right)
{
Vector1 vector = new Vector1(0);
vector.Length = Left.Length;
return vector;
}
}
public class Vec1Math
{
public static Vector1 Exp(Vec1 x)
{
Vector1 vector = new Vector1(0);
vector.Length = x.Length;
if (vector.Length == 0) throw new Exception("Length is zero");
return vector;
}
public static Vector1 Sqrt(Vec1 x)
{
Vector1 vector = new Vector1(0);
vector.Length = x.Length;
if (vector.Length == 0) throw new Exception("Length is zero");
return vector;
}
}
//This functions must be called in a loop:
private double MyFunction(int Iterations)
{
double a = 1;
Vector1 xx, x, res;
x = new Vector1(100);
int counter = Environment.TickCount;
for (int i = 0; i < Iterations; i++)
{
xx = x*x;
res = Vec1Math.Sqrt(-0.5 * a * xx) * xx * Vec1Math.Exp(4 *
Math387.INVTWOPI * x);
}
return Environment.TickCount - counter;
}
//here is the main event
private void button1_Click(object sender, System.EventArgs e) {
iters = 1000000;
for (int i=0;i<=5;i++)
{
Function(iters);
iters /= 10;
Refresh;
}
It takes on my machine about 5 seconds before the error surfaces...
It may be that due to different loads, the time will be different on
other machines... Increase the Iters count if it finishes too quick
or does not trigger a Length = 0 exception... The loop must run
at least for a few seconds... This was done on Pentium M 1.7GHz
and VS2005.
Thanks!
Atmapuri