Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HRESULT.ToString() is not thread-safe due to mutable global state. #419

Closed
ArchibaldMacdonald-cyacomb opened this issue Aug 3, 2023 · 2 comments

Comments

@ArchibaldMacdonald-cyacomb

Describe the bug and how to reproduce

HRESULT.ToString() relies on a string cache that is not thread-safe, so when calling HRESULT.ToString() from different threads, an exception is triggered when updating this cache.

What code is involved

This can be easily triggered with the following snippet:

using System.Diagnostics;
using Vanara.PInvoke;

var a1 = Task.Run(() => { for (int i = 0; i < 100; ++i) { Debug.Print(new HRESULT().ToString()); } });
var a2 = Task.Run(() => { for (int i = 0; i < 100; ++i) { Debug.Print(new HRESULT().ToString()); } });
Task.WaitAll(a1, a2);

Expected behavior

Two separate HRESULT objects, that exist separately on different threads, should not interfere with one another.

Screenshots

Code that triggers exception:
image

Exception source:

image

@dahall
Copy link
Owner

dahall commented Aug 7, 2023

I just pushed a new version 3.4.16 that should fix this. Please test and confirm.

@ArchibaldMacdonald-cyacomb
Copy link
Author

Thanks for the fast turnaround. I can confirm it is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants