wxMSW crashes when attempting to create a wxGraphicsContext for a D2DRenderer using a very large bitmap (Issue #26041)

7 views
Skip to first unread message

David Hansel

unread,
Dec 19, 2025, 1:23:21 PM (15 hours ago) Dec 19
to wx-...@googlegroups.com, Subscribed
dhansel created an issue (wxWidgets/wxWidgets#26041)

This is not critical but a case where it would be good to handle invalid inputs more gracefully.

To reproduce, add the following code at the end of the MyFrame() constructor of the "minimal" example:

wxBitmap bitmap;
bitmap.Create(20000, 20000);
wxMemoryDC dc;
dc.SelectObject(bitmap);
wxGraphicsContext* c = wxGraphicsRenderer::GetDirect2DRenderer()->CreateContext(dc);
delete c;

and add include "wx/graphics.h" at the top.

This should crash the application on startup. If it does not you may have to teak the bitmap size.
It has to be small enough such that the wxBitmap::Create() call succeeds but large enough for
wxGraphicsRenderer()::CreateContext() to fail. This may vary on different systems depending on
RAM size and graphics capabilities. I only have tested it on one system and the above values
reproduce the issue there.

In a Debug build this initially causes an assertion fail at:

 	minimal.exe!wxD2DDCRenderTargetResourceHolder::DoAcquireResource() Line 3765	C++
 	minimal.exe!wxD2DResourceHolder<ID2D1RenderTarget>::AcquireResource() Line 763	C++
 	minimal.exe!wxD2DResourceHolder<ID2D1RenderTarget>::GetD2DResource() Line 754	C++
 	minimal.exe!wxD2DContext::EnsureInitialized() Line 4777	C++
 	minimal.exe!wxD2DContext::Init() Line 4141	C++
 	minimal.exe!wxD2DContext::wxD2DContext(wxGraphicsRenderer * renderer, ID2D1Factory * direct2dFactory, HDC__ * hdc, const wxDC * dc, D2D1_ALPHA_MODE alphaMode) Line 4109	C++
 	minimal.exe!wxD2DRenderer::CreateContext(const wxMemoryDC & dc) Line 5142	C++
 	minimal.exe!MyFrame::MyFrame(const wxString & title) Line 185	C++

because ID2D1DCRenderTarget::BindDC() fails with a E_INVALIDARG status.
If I continue after the assertion failure then it crashes in

	minimal.exe!wxD2DContext::EnsureInitialized() Line 4778	C++
 	minimal.exe!wxD2DContext::Init() Line 4141	C++
 	minimal.exe!wxD2DContext::wxD2DContext(wxGraphicsRenderer * renderer, ID2D1Factory * direct2dFactory, HDC__ * hdc, const wxDC * dc, D2D1_ALPHA_MODE alphaMode) Line 4109	C++
 	minimal.exe!wxD2DRenderer::CreateContext(const wxMemoryDC & dc) Line 5142	C++
 	minimal.exe!MyFrame::MyFrame(const wxString & title) Line 185	C++

because GetRenderTarget() returns NULL (m_cachedRenderTarget is NULL because GetD2DResource() failed).

As I said this is not critical, I should not be creating such large bitmaps.
However, it is hard to know what is "too large" - it would be helpful if the CreateContext()
function returned NULL if it fails in this case (instead of crashing)...


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/26041@github.com>

Reply all
Reply to author
Forward
0 new messages