From: Matthew Fernandez Date: Sat, 27 Nov 2021 03:43:33 +0000 (-0800) Subject: GDI+ plugin: rewrite 'FileStream' reference counting to be thread safe X-Git-Tag: 2.50.0~3^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0db8dc7480dd4a544c6f39222ddfc3d905eb7061;p=graphviz GDI+ plugin: rewrite 'FileStream' reference counting to be thread safe Based on Microsoft sample code,¹ it seems like the expectation is that consumers of the `IStream` API may expect to concurrently manipulate reference counts. This change guards against this by making the referencing counting code use lock-free atomics, based on the same Microsoft sample. Note that this is essentially proactively addressing a latent issue as it is, in general, unsafe to use Graphviz libraries or plugins in a multithreaded environment right now. ¹ https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms752876(v=vs.85) --- diff --git a/plugin/gdiplus/FileStream.cpp b/plugin/gdiplus/FileStream.cpp index 0fe700814..aa2d030d6 100644 --- a/plugin/gdiplus/FileStream.cpp +++ b/plugin/gdiplus/FileStream.cpp @@ -53,12 +53,12 @@ HRESULT FileStream::QueryInterface( ULONG FileStream::AddRef() { - return ++_ref; + return (ULONG)InterlockedIncrement((ULONG*)&_ref); } ULONG FileStream::Release() { - ULONG ref = --_ref; + ULONG ref = (ULONG)InterlockedDecrement((ULONG*)&_ref); if (ref == 0) delete this; return ref;