B
Bill Butler
Atmapuri said:Hi!
If you would have read a few posts down, you would have found it.
I saw it right after I posted...It wasn't complete but it was enought to
find your problem.
The problem in your code is ~Vector1() [~Outer() in the sample below]
You assume that since the Outer object is being GCed it is safe to
assume that nobody holds a reference to the inner object.
This is a bad assumption.
In the sample program below this is demonstrated by the call
Func(outer.Inner);
We pass a reference to the inner object to Func.
GC is kicked off while inside of func (by the call to Outer outer = new
Outer(); )
outer is fair game for GC but Inner is still alive.
I don't know if your real example requires ~Vector1().
If not...let the framework handle it
Good Luck
Bill
Here is a short but complete program that demonstrates your problem
-------------------------
using System;
public class Inner
{
public int Value = 100;
}
public class Outer
{
public Inner Inner;
public Outer()
{
Inner = new Inner();
}
~Outer() {Inner.Value = 0;}
}
public class Test
{
public static void Main()
{
for (int i=0;i<=10000000;i++)
{
Outer outer = new Outer();
Func(outer.Inner); // since outer is not referenced again it is
fair game for GC
// outer.Inner.Value++; // This statement can fix the problem by
referencing outer
}
}
public static void Func(Inner Inner)
{
Outer outer = new Outer(); // this line triggers the GC to occur
eventually
if (Inner.Value == 0){ throw new Exception("Ooops"); }
}
}