AccessViolation profiling unittest with vstest

Use this forum for questions on how to use .NET Memory Profiler and how to analyse memory usage.
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


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 ...");



    // 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


        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.
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
Ebene:         Fehler
Benutzer:      Nicht zutreffend
Computer:      xxx
Name der fehlerhaften Anwendung: testhost.x86.exe, Version:, 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: 
<Event xmlns="">
    <Provider Name="Application Error" />
    <EventID Qualifiers="0">1000</EventID>
    <TimeCreated SystemTime="2021-06-28T13:36:31.6334902Z" />
    <Correlation />
    <Execution ProcessID="0" ThreadID="0" />
    <Security />
    <Data>C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\Extensions\TestPlatform\testhost.x86.exe</Data>
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: No registered users and 11 guests