MemAssertion methods act differently if evaluated?

Use this forum for questions on how to use .NET Memory Profiler and how to analyse memory usage.
Post Reply
kellyconway
Posts: 10
Joined: Wed Nov 09, 2011 8:49 pm

MemAssertion methods act differently if evaluated?

Post by kellyconway » Wed Nov 09, 2011 9:16 pm

Hi. I'm currently using a trial version of 4.0.114.0, and have requested that my company purchase a few full versions for our use.

After having used it to diagnose a memory leak, I want to use it as part of our automated build process to detect and notify us of future potential memory leaks. I've created a small test app that creates a baseline snapshot, creates some objects, asserts that those objects do not exist (to test assertion failures), and then takes a final snapshot.

I'd like my code to do 2 things when an assertion fails:

1. Log the failed assertion

2. If I later open the generated profiler session in the UI, have it show the "Potential memory leak" warning

I've been able to easily get either #1 or #2 in isolation, but have noticed something odd when I try to get both #1 and #2 using a single assertion.

If I do anything to evaluate the bool value returned from a MemAssertion.NoNewInstances method call, #2 does not happen (i.e., the UI doesn't show the "Potential memory leak" warning when I open the generated session file).

I've tried both:

Code: Select all

if (!MemAssertion.NoNewInstances(...))
    File.Create("houston.txt");    //We will do logging here instead
and:

Code: Select all

bool assertionResult = MemAssertion.NoNewInstances(...);
if (!assertionResult)
    File.Create("houston.txt");    //We will do logging here instead
In either case, when the assertion fails, I get my file created (good), but the session file does not contain the "Potential memory leak" warning (bad).

If I make a "naked" assertion (i.e., don't check or save the return value), I do get the "Potential memory leak" warning in the generated session (good), but then I have to make the assertion twice (bad) in order to do something conditioned on whether the assertion failed. As in:

Code: Select all

MemAssertion.NoNewInstances(...);
if (!MemAssertion.NoNewInstances(...)
    File.Create("houston.txt");    //We will do logging here instead
My question becomes is this the way assertions are intended to function or should I be able to evaluate the bool return value from one and still have the "Potential memory leak" warning appear in the generated session?

kellyconway
Posts: 10
Joined: Wed Nov 09, 2011 8:49 pm

Re: MemAssertion methods act differently if evaluated?

Post by kellyconway » Wed Nov 09, 2011 9:42 pm

Hmmm. Often, it seems the solution to an issue is to post about it online. Sure enough, after having submitted my op, I went back to the project and found that the issue was resolved. No idea why it had been failing preivously, but it's working fine now. :)

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Re: MemAssertion methods act differently if evaluated?

Post by Andreas Suurkuusk » Thu Nov 10, 2011 7:51 am

This is most likely caused by the "Suspend thread until memory leak is handled" setting (see screenshot). If this option is not enabled, your program will probably exit before the profiler manages to collect the snapshot (since you are running a small test program).
MemLeakSuspend.png
MemLeakSuspend.png (45.23 KiB) Viewed 7230 times
Try to enable this setting and see if it works better. The reason that it works when you have two assertions, is that only one assertion can be handled at the time, so the second assertion will wait for the first assertion to be handled and the snapshot will be collected.
Best regards,

Andreas Suurkuusk
SciTech Software AB

kellyconway
Posts: 10
Joined: Wed Nov 09, 2011 8:49 pm

Re: MemAssertion methods act differently if evaluated?

Post by kellyconway » Thu Nov 10, 2011 2:04 pm

Excellent support, Andreas. Thanks!

Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests