Page 1 of 1

MemAssertion.NoNewInstances(), NoNewInstancesExcept(Type)

Posted: Wed Mar 15, 2006 10:25 pm
by scot.prichard@med.ge.com
Why does this code Assert?

class Test { }

class Program
{
static void Main(string[] args)
{
MemProfiler.FullSnapShot();
MemProfiler.FullSnapShot();
MemAssertion.NoNewInstances();
MemProfiler.FullSnapShot();
using (MemAssertion.BeginAssertions())
{
MemAssertion.NoNewInstances();
}

Test test = new Test();
MemAssertion.NoNewInstancesExcept(typeof(Test));
}
}

Posted: Thu Mar 16, 2006 1:11 pm
by Andreas Suurkuusk
Using MemAssertion.NoNewInstances and MemAssertion.NoNewInstancesExcept require that you perform the assertions in an "assertions session", otherwise it is very likely that memory leaks will be falsely detected. So in your case, the first and the third assertion will fail, since short-lived instances will be created during the assertion itself. However, the second assertion should not fail.

It appears that assertions didn't work when using a MemProfiler.FullSnapshot as comparison snapshot. This was a bug in the profiler which has now been fixed and an updated version of the profiler has been released. You can download this version (v2.6.82) from http://memprofiler.com/download.aspx.