From 6c6be3c26335644bb65913a4db03388ec6ff4aab Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Tue, 27 Aug 2019 16:29:04 -0400 Subject: Two fixes to avoid random crash on destruction of GLRenderer (#1038) * Two fixes to avoid random crash on destruction of GLRenderer * Use of a weak reference from objects created by GLRenderer, such that GLRenderer dtor can disable those objects assuming GLRenderer is live * Make sure window is not destroyed before the renderer * Used WeakSink for weak pointer. --- source/core/slang-smart-pointer.h | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'source') diff --git a/source/core/slang-smart-pointer.h b/source/core/slang-smart-pointer.h index ebc6f20b0..1bbcffe47 100644 --- a/source/core/slang-smart-pointer.h +++ b/source/core/slang-smart-pointer.h @@ -249,8 +249,27 @@ namespace Slang private: T* pointer; - }; + + // Helper type for implementing weak pointers. The object being pointed at weakly creates a WeakSink object + // that other objects can reference and share. When the object is destroyed it detaches the sink + // doing so will make other users call to 'get' return null. Thus any user of the WeakSink, must check if the weakly pointed to + // things pointer is nullptr before using. + template + class WeakSink : public RefObject + { + public: + WeakSink(T* ptr): + m_ptr(ptr) + { + } + + SLANG_FORCE_INLINE T* get() const { return m_ptr; } + SLANG_FORCE_INLINE void detach() { m_ptr = nullptr; } + + private: + T* m_ptr; + }; } #endif -- cgit v1.2.3