AccessViolation profiling unittest with vstest

Use this forum for questions on how to use .NET Memory Profiler and how to analyse memory usage.
Post Reply
sunnyboy
Posts: 3
Joined: Mon Jun 21, 2021 2:12 pm

AccessViolation profiling unittest with vstest

Post by sunnyboy » Mon Jun 28, 2021 2:18 pm

Execution my unittest will curiously fail in 95% times of execution. It is very suspect to me that this happens not in 100%.

This is my unittest:

Code: Select all

MemProfiler.AreAssertionsEnabled.Should().BeTrue();
MemProfiler.IsProfiling.Should().BeTrue();

using (MemProfiler.BeginTest(testName))
{
    /* NOTE:
     * When profiling a debug build of a program, the scope of the local variables is often longer than it might appear.
     * This will prevent the instances from being collected, and the assertion will fail, falsely identifying the instances as a memory leak.
     */
    void Iteration()
    {
        TestContext.WriteLine("Execute SuT ...");

        doWork();
    }

    Iteration();

    // get base snapshot
    TestContext.WriteLine("Create base snapshot");

    using (MemSnapshot fastSnapshot = MemProfiler.FastSnapshot())
    using (MemSnapshot baseSnapshot = MemProfiler.FullSnapshot($"{testName} - Base snapshot")) // separate snapshot to compare in .NET Memory Profiler
    {
        fastSnapshot.Should().NotBeNull().And.NotBe(MemSnapshot.Empty);
        baseSnapshot.Should().NotBeNull().And.NotBe(MemSnapshot.Empty);

        Iteration();

        MemProfiler.FullSnapshot($"{testName} - Comparison snapshot").Dispose(); // separate snapshot to compare in .NET Memory Profiler

        using (AssertionsDefinition ad = MemAssertion.BeginAssertionsDefinition(fastSnapshot))
        {
            TestContext.WriteLine("Assert there are no new relevant instances ...");

            // No new instances of any type in the Datev namespace should exist
            ad.NoNewInstances("Nobody.*", true).NoNewInstances("Client.*", true)
              // Since a timeobject is returned, we must allow 1 new instance of that type to be created.
              // The MaxNewInstances assertion will override the NoNewInstances assertion above.
              .MaxNewInstances(typeof(TaskResponse), 1);

            // The AssertionsDefinition has been built, let’s perform the actual assertion.
            MemAssertion.Assert(ad);
        }
    }
}
I start execution the unittest with the following command line:

Code: Select all

VSTest.Console.exe Nobody.Test.dll --Settings:Nobody.Test\MemoryTest.runsettings
The problematic line is:

Code: Select all

using (MemSnapshot baseSnapshot = MemProfiler.FullSnapshot($"{testName} - Base snapshot"))
. Creating a full snapshot causes an access violation error:

Code: Select all

Protokollname: Application
Quelle:        Application Error
Datum:         28.06.2021 15:36:31
Ereignis-ID:   1000
Aufgabenkategorie:(100)
Ebene:         Fehler
Schlüsselwörter:Klassisch
Benutzer:      Nicht zutreffend
Computer:      xxx
Beschreibung:
Name der fehlerhaften Anwendung: testhost.x86.exe, Version: 15.0.0.0, Zeitstempel: 0xe16c7da8
Name des fehlerhaften Moduls: clr.dll, Version: 4.8.4360.0, Zeitstempel: 0x606e7038
Ausnahmecode: 0xc0000005
Fehleroffset: 0x0033c7fc
ID des fehlerhaften Prozesses: 0x5660
Startzeit der fehlerhaften Anwendung: 0x01d76c22883da4c8
Pfad der fehlerhaften Anwendung: C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\Extensions\TestPlatform\testhost.x86.exe
Pfad des fehlerhaften Moduls: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Berichtskennung: 8f16caa2-2881-444b-aa56-ad424998de35
Vollständiger Name des fehlerhaften Pakets: 
Anwendungs-ID, die relativ zum fehlerhaften Paket ist: 
Ereignis-XML:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
  <System>
    <Provider Name="Application Error" />
    <EventID Qualifiers="0">1000</EventID>
    <Version>0</Version>
    <Level>2</Level>
    <Task>100</Task>
    <Opcode>0</Opcode>
    <Keywords>0x80000000000000</Keywords>
    <TimeCreated SystemTime="2021-06-28T13:36:31.6334902Z" />
    <EventRecordID>24062</EventRecordID>
    <Correlation />
    <Execution ProcessID="0" ThreadID="0" />
    <Channel>Application</Channel>
    <Computer>xxx</Computer>
    <Security />
  </System>
  <EventData>
    <Data>testhost.x86.exe</Data>
    <Data>15.0.0.0</Data>
    <Data>e16c7da8</Data>
    <Data>clr.dll</Data>
    <Data>4.8.4360.0</Data>
    <Data>606e7038</Data>
    <Data>c0000005</Data>
    <Data>0033c7fc</Data>
    <Data>5660</Data>
    <Data>01d76c22883da4c8</Data>
    <Data>C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\Extensions\TestPlatform\testhost.x86.exe</Data>
    <Data>C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll</Data>
    <Data>8f16caa2-2881-444b-aa56-ad424998de35</Data>
    <Data>
    </Data>
    <Data>
    </Data>
  </EventData>
</Event>
It would be great if you could help me figure out what I'm doing wrong.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 15 guests