Crashes in updateColourBrush / direct2D [solved, not a JUCE bug]

Hi, after updating my app from juce 8.0.10 to 8.0.13 (and staring to use VBlank animations), I’ve been getting a few crash reports from windows users (null pointer dereference) , that share the same stack trace:

public:  void __cdecl juce::Direct2DGraphicsContext::SavedState::updateColourBrush(void)+0x13]
public:  virtual void __cdecl juce::Direct2DGraphicsContext::restoreState(void)+0x26]
public: __cdecl juce::Graphics::ScopedSaveState::~ScopedSaveState(void)+0x1d]
private: void __cdecl juce::Component::paintComponentAndChildren(class juce::Graphics &, class juce::Component::OpaqueLayer &, struct juce::ComponentPaintDiagnostics &)+0x1f0]
private: void __cdecl juce::Component::paintEntireComponent(class juce::Graphics &, bool, class juce::Component::OpaqueLayer &, struct juce::ComponentPaintDiagnostics &)+0x1b0]
public:  void __cdecl juce::Component::paintEntireComponent(class juce::Graphics &, bool)+0x113]
public:  void __cdecl juce::ComponentPeer::handlePaint(class juce::LowLevelGraphicsContext &)+0x103]
public:  __cdecl <lambda_94fc728bae5689456220642f2a5a05c7>::operator()(void) const+0x5c]
public:  virtual void __cdecl juce::D2DRenderContext::onVBlank(void)+0x23]
public:  virtual void __cdecl juce::HWNDComponentPeer::onVBlank(double)+0x27]
private: virtual void __cdecl juce::VBlankThread::handleAsyncUpdate(void)+0x3f]
private: void __cdecl juce::InternalMessageQueue::dispatchMessages(void)+0x9f]
1 Like

I haven’t been able to repro this issue so far.

Just to be sure, are you using the restoreState() and saveState() functions directly? Using ScopedSaveState in that case would be preferable as it would ensure that restoreState() isn’t accidentally called more times.

Wow thank you so much Attila you just found the bug (on my side).

I had a paint() method that has been split in paint + paintOverChildren , and there was a saveState in paint and restoreState in paintOverChildren, so no surprise that this is causing issues.

Sorry for the noise.

I can try to turn this invalid bug report into something useful:

Maybe it would be possible for JUCE to detect unbalanced saveState / restoreState calls in each paint() call ?

Similarly I would be useful if JUCE triggered a jassert when a juce::Image is being accessed by two Graphics objects, as I have discovered recently a few issues caused by this kind of mistake, that were only triggered on windows with direct2D.