]> granicus.if.org Git - graphviz/commitdiff
GDI+ plugin: rewrite 'FileStream' reference counting to be thread safe
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 27 Nov 2021 03:43:33 +0000 (19:43 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 4 Dec 2021 02:59:17 +0000 (18:59 -0800)
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)

plugin/gdiplus/FileStream.cpp

index 0fe70081434617d7fcd57302394263abd7b80faa..aa2d030d6fc785bb0973a8884c04f6bbe3a51d3d 100644 (file)
@@ -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;