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

Stop using std::map since cleanup races with the finalizer during shutdown. #38375

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,89 @@
#define THROW_IF_FAILED(exp) { hr = exp; if (FAILED(hr)) { ::printf("FAILURE: 0x%08x = %s\n", hr, #exp); throw hr; } }
#define THROW_FAIL_IF_FALSE(exp) { if (!(exp)) { ::printf("FALSE: %s\n", #exp); throw E_FAIL; } }

#include <map>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it just the debug iterators causing issues? Can we set _ITERATOR_DEBUG_LEVEL?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly. However @AndyAyersMS statement above about the state of the CRT being the real issue is why we may want to just remove its usage entirely.

#include <string>

namespace
{
class DispIDToStringMap
{
struct Pair
{
DISPID id;
WCHAR value[128];
};
Pair _pairs[8];
const Pair* _end;

public:
DispIDToStringMap()
: _pairs{}
, _end{ _pairs + ARRAYSIZE(_pairs) }
{
for (auto curr = _pairs; curr != _end; ++curr)
curr->id = DISPID_UNKNOWN;
}

const WCHAR* Find(_In_ DISPID id)
{
for (auto curr = _pairs; curr != _end; ++curr)
{
if (curr->id == id)
return curr->value;
}

return nullptr;
}

void Insert(_In_ DISPID id, _In_z_ const WCHAR* value)
{
if (id == DISPID_UNKNOWN)
throw E_UNEXPECTED;

for (auto curr = _pairs; curr != _end; ++curr)
{
if (curr->id == DISPID_UNKNOWN)
{
curr->id = id;
size_t len = ::wcslen(value) + 1; // Include null
::memcpy(curr->value, value, len * sizeof(value[0]));
return;
}
}

throw E_UNEXPECTED;
}

void Erase(_In_ DISPID id)
{
for (auto curr = _pairs; curr != _end; ++curr)
{
if (curr->id == id)
{
curr->id = DISPID_UNKNOWN;
break;
}
}
}
};

class EventSink : public UnknownImpl, public TestingEvents
{
std::map<DISPID, std::wstring> _firedEvents;
DispIDToStringMap _firedEvents;

public:
void ResetFiredState(_In_ DISPID id)
{
_firedEvents.erase(id);
_firedEvents.Erase(id);
}

bool DidFire(_In_ DISPID id, _Out_ std::wstring& message)
{
auto iter = _firedEvents.find(id);
if (iter == std::end(_firedEvents))
auto value = _firedEvents.Find(id);
if (value == nullptr)
return false;

message = iter->second;
message = value;
return true;
}

Expand Down Expand Up @@ -112,7 +173,7 @@ namespace
if (msgMaybe->vt != VT_BSTR)
return E_INVALIDARG;

_firedEvents.insert({ dispId, msgMaybe->bstrVal });
_firedEvents.Insert(dispId, msgMaybe->bstrVal);
return S_OK;
}

Expand Down