Unhandled exception thrown: read access violation. this->m_ptr->**** was 0x7FFD376680B8 using WinRT APIs

Scrivano Dev 6 Reputation points
2022-05-10T13:07:20.847+00:00

I'm trying to find the root cause of a crash that is affecting some users of my Win32 application. The crash seems to affect a major subset of users and is caused by an unhandled exception that results in:

Unhandled exception thrown: read access violation.
this->m_ptr->**** was 0x7FFD376680B8.

The minidump file summary outputs:

Exception Code: 0xC0000005
Exception Information: The thread tried to read from or write to a virtual address for which it does not have the appropriate access.

Upon loading debug symbols, the call stack for the crash is the following:

App.exe!winrt::Windows::Foundation::IUnknown::unconditional_release_ref() Line 2040	C++	Non-user code. Symbols loaded.    
[Inline Frame] App.exe!winrt::Windows::Foundation::IUnknown::release_ref() Line 2034	C++	Symbols loaded.    
[Inline Frame] App.exe!winrt::Windows::Foundation::IUnknown::{dtor}() Line 1939	C++	Symbols loaded.    
App.exe!InkCompositionHost::~InkCompositionHost() Line 34	C++	Symbols loaded.  

The crash happens when the application exits and the destructor of the InkCompositionHost class is called. This class is simple wrapper to make use of Visual Layer APIs. The class is quite isolated from the rest of the code and is fairly short:

   // inkcompositionhost.h  
     
   #pragma once  
   #include <winrt/Windows.UI.Composition.Desktop.h>  
   #include <windows.ui.composition.interop.h>  
   #include <dispatcherqueue.h>  
   #include <winrt/Windows.Foundation.Collections.h>  
   #include <winrt/windows.foundation.h>  
   #include <stdint.h>  
     
   class InkCompositionHost  
   {  
   public:  
       ~InkCompositionHost();  
     
       static InkCompositionHost* GetInstance();  
     
       void Initialize(HWND hwnd);  
     
       static winrt::Windows::UI::Composition::InkTrailPoint MakeInkTrailPoint(float x, float y, float radius)  
       {  
           winrt::Windows::Foundation::Point point {x, y};  
           return winrt::Windows::UI::Composition::InkTrailPoint{point, radius};  
       }  
     
       int AddInkTrailPoints(std::vector<winrt::Windows::UI::Composition::InkTrailPoint> points);  
       void StartInkTrail(uint8_t red, uint8_t blue, uint8_t green, uint8_t alpha);  
       void ResetInkTrail();  
       void RemoveAddedInkTrails();  
     
       std::vector<int> inkTrailsAdded;  
       bool isSupported() const;  
     
   private:  
       InkCompositionHost();  
     
       void EnsureDispatcherQueue();  
       void CreateDesktopWindowTarget(HWND window);  
       void CreateCompositionRoot();  
     
       bool m_isSupported = false;  
     
       winrt::Windows::UI::Composition::Compositor m_compositor{ nullptr };  
       winrt::Windows::System::DispatcherQueueController m_dispatcherQueueController{ nullptr };  
       winrt::Windows::UI::Composition::Desktop::DesktopWindowTarget m_target{ nullptr };  
   };  
     
   // inkcompositionhost.cpp  
     
   #include <unknwn.h>  
   #include <winrt/base.h>  
   #include <winrt/windows.foundation.metadata.h>  
   #include "inkcompositionhost.h"  
     
   #include <winrt/Windows.System.h>  
   #include <ShellScalingAPI.h>  
   #include <DispatcherQueue.h>  
   #include <QDebug>  
     
   using namespace winrt;  
   using namespace Windows::System;  
   using namespace Windows::UI;  
   using namespace Windows::UI::Composition;  
   using namespace Windows::UI::Composition::Desktop;  
   using namespace Windows::Foundation::Numerics;  
   using namespace Windows::Foundation::Metadata;  
     
   InkCompositionHost::InkCompositionHost()  
   {  
       hstring inkTrailVisualTypeString {L"Windows.UI.Composition.DelegatedInkTrailVisual"};  
       m_isSupported = ApiInformation::IsTypePresent(inkTrailVisualTypeString);  
       qInfo () << this << "Delegated Ink Trails supported:" << m_isSupported;  
   }  
     
   InkCompositionHost* InkCompositionHost::GetInstance()  
   {  
       static InkCompositionHost instance;  
       return &instance;  
   }  
     
   InkCompositionHost::~InkCompositionHost()  
   {  
   }  
     
   void InkCompositionHost::Initialize(HWND hwnd)  
   {  
       if(!isSupported())  
           return;  
     
       EnsureDispatcherQueue();  
       if (m_dispatcherQueueController) m_compositor = Compositor();  
     
       if (m_compositor)  
       {  
           CreateDesktopWindowTarget(hwnd);  
           CreateCompositionRoot();  
       }  
   }  
     
   void InkCompositionHost::EnsureDispatcherQueue()  
   {  
       namespace abi = ABI::Windows::System;  
     
       if (m_dispatcherQueueController == nullptr)  
       {  
           namespace abi = ABI::Windows::System;  
     
           DispatcherQueueOptions options  
           {  
               sizeof(DispatcherQueueOptions),  
                       DQTYPE_THREAD_CURRENT,  
                       DQTAT_COM_STA  
           };  
     
           DispatcherQueueController controller{ nullptr };  
           check_hresult(CreateDispatcherQueueController(options, reinterpret_cast<abi::IDispatcherQueueController**>(put_abi(controller))));  
           m_dispatcherQueueController = controller;  
       }  
   }  
     
   void InkCompositionHost::CreateDesktopWindowTarget(HWND window)  
   {  
       namespace abi = ABI::Windows::UI::Composition::Desktop;  
     
       auto interop = m_compositor.as<abi::ICompositorDesktopInterop>();  
       DesktopWindowTarget target{ nullptr };  
       check_hresult(interop->CreateDesktopWindowTarget(window, false, reinterpret_cast<abi::IDesktopWindowTarget**>(put_abi(target))));  
       m_target = target;  
   }  
     
   void InkCompositionHost::CreateCompositionRoot()  
   {  
       auto root = m_compositor.CreateContainerVisual();  
       root.RelativeSizeAdjustment({ 1.0f, 1.0f });  
       m_target.Root(root);  
   }  
     
   bool InkCompositionHost::isSupported() const  
   {  
       return m_isSupported;  
   }  
     
   int InkCompositionHost::AddInkTrailPoints(std::vector<winrt::Windows::UI::Composition::InkTrailPoint> points)  
   {  
       if(!isSupported())  
           return -1;  
     
       if(!m_target.Root()){  
           return -1;  
       }  
       int id = -1;  
       auto visuals = m_target.Root().as<ContainerVisual>().Children();  
     
       if(visuals.Count() > 0){  
           auto inkTrail = visuals.First().Current().try_as<DelegatedInkTrailVisual>();  
           if(inkTrail){  
               id = inkTrail.AddTrailPoints(points);  
           }  
           else{  
               qCritical() << "ERROR: Cannot find ink trail in visual tree";  
           }  
       }  
       if(id != -1){  
           inkTrailsAdded.push_back(id);  
       }  
       return id;  
   }  
     
   void InkCompositionHost::StartInkTrail(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)  
   {  
       if(!isSupported())  
           return;  
     
       auto inkTrail = DelegatedInkTrailVisual::Create(m_compositor);  
       inkTrail.StartNewTrail(Color{ alpha, red, green, blue});  
       auto visuals = m_target.Root().as<ContainerVisual>().Children();  
       visuals.InsertAtTop(inkTrail);  
   }  
     
   void InkCompositionHost::ResetInkTrail()  
   {  
       if(!isSupported())  
           return;  
     
       RemoveAddedInkTrails();  
       auto visuals = m_target.Root().as<ContainerVisual>().Children();  
       visuals.RemoveAll();  
   }  
     
   void InkCompositionHost::RemoveAddedInkTrails()  
   {  
       if(!isSupported())  
           return;  
     
       auto visuals = m_target.Root().as<ContainerVisual>().Children();  
       auto inkTrail = visuals.First().Current().try_as<DelegatedInkTrailVisual>();  
     
       if(inkTrail){  
           for(auto const& id: inkTrailsAdded){  
               inkTrail.RemoveTrailPoints(id);  
           }  
       }  
   }  
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,519 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,973 questions
{count} votes