Skip to content
Merged
Show file tree
Hide file tree
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
26 changes: 23 additions & 3 deletions strings/base_agile_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,39 @@ WINRT_EXPORT namespace winrt

namespace winrt::impl
{
struct agile_ref_fallback final : IAgileReference
template<bool UseModuleLock>
struct module_lock_updater;

template<>
struct module_lock_updater<true>
{
module_lock_updater() noexcept
{
++get_module_lock();
}

~module_lock_updater() noexcept
{
--get_module_lock();
}
};

template<>
struct module_lock_updater<false> {};

using update_module_lock = module_lock_updater<true>;

struct agile_ref_fallback final : IAgileReference, update_module_lock
{
agile_ref_fallback(com_ptr<IGlobalInterfaceTable>&& git, uint32_t cookie) noexcept :
m_git(std::move(git)),
m_cookie(cookie)
{
++get_module_lock();
}

~agile_ref_fallback() noexcept
{
m_git->RevokeInterfaceFromGlobal(m_cookie);
--get_module_lock();
}

int32_t __stdcall QueryInterface(guid const& id, void** object) noexcept final
Expand Down
16 changes: 2 additions & 14 deletions strings/base_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@
namespace winrt::impl
{
template <typename T, typename H>
struct implements_delegate : abi_t<T>, H
struct implements_delegate : abi_t<T>, H, update_module_lock
{
implements_delegate(H&& handler) : H(std::forward<H>(handler))
{
++get_module_lock();
}

~implements_delegate() noexcept
{
--get_module_lock();
}

int32_t __stdcall QueryInterface(guid const& id, void** result) noexcept final
Expand Down Expand Up @@ -98,16 +92,10 @@ namespace winrt::impl
};

template <typename H, typename R, typename... Args>
struct variadic_delegate final : variadic_delegate_abi<R, Args...>, H
struct variadic_delegate final : variadic_delegate_abi<R, Args...>, H, update_module_lock
{
variadic_delegate(H&& handler) : H(std::forward<H>(handler))
{
++get_module_lock();
}

~variadic_delegate() noexcept
{
--get_module_lock();
}

R invoke(Args const& ... args) final
Expand Down
8 changes: 1 addition & 7 deletions strings/base_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,12 @@ namespace winrt::impl
return ((int32_t)((x) | 0x10000000));
}

struct error_info_fallback final : IErrorInfo, IRestrictedErrorInfo
struct error_info_fallback final : IErrorInfo, IRestrictedErrorInfo, update_module_lock
{
error_info_fallback(int32_t code, void* message) noexcept :
m_code(code),
m_message(*reinterpret_cast<winrt::hstring*>(&message))
{
++get_module_lock();
}

~error_info_fallback() noexcept
{
--get_module_lock();
}

int32_t __stdcall QueryInterface(guid const& id, void** object) noexcept final
Expand Down
36 changes: 14 additions & 22 deletions strings/base_implements.h
Original file line number Diff line number Diff line change
Expand Up @@ -522,18 +522,18 @@ namespace winrt::impl
}
};

template <bool Agile>
template <bool Agile, bool UseModuleLock>
struct weak_ref;

template <bool Agile>
template <bool Agile, bool UseModuleLock>
struct weak_source_producer;

template <bool Agile>
struct weak_source final : IWeakReferenceSource
template <bool Agile, bool UseModuleLock>
struct weak_source final : IWeakReferenceSource, module_lock_updater<UseModuleLock>
{
weak_ref<Agile>* that() noexcept
weak_ref<Agile, UseModuleLock>* that() noexcept
{
return static_cast<weak_ref<Agile>*>(reinterpret_cast<weak_source_producer<Agile>*>(this));
return static_cast<weak_ref<Agile, UseModuleLock>*>(reinterpret_cast<weak_source_producer<Agile, UseModuleLock>*>(this));
}

int32_t __stdcall QueryInterface(guid const& id, void** object) noexcept final
Expand Down Expand Up @@ -566,15 +566,15 @@ namespace winrt::impl
}
};

template <bool Agile>
template <bool Agile, bool UseModuleLock>
struct weak_source_producer
{
protected:
weak_source<Agile> m_source;
weak_source<Agile, UseModuleLock> m_source;
};

template <bool Agile>
struct weak_ref final : IWeakReference, weak_source_producer<Agile>
template <bool Agile, bool UseModuleLock>
struct weak_ref final : IWeakReference, weak_source_producer<Agile, UseModuleLock>
{
weak_ref(unknown_abi* object, uint32_t const strong) noexcept :
m_object(object),
Expand Down Expand Up @@ -678,10 +678,10 @@ namespace winrt::impl
}

private:
template <bool T>
template <bool T, bool U>
friend struct weak_source;

static_assert(sizeof(weak_source_producer<Agile>) == sizeof(weak_source<Agile>));
static_assert(sizeof(weak_source_producer<Agile, UseModuleLock>) == sizeof(weak_source<Agile, UseModuleLock>));

unknown_abi* m_object{};
std::atomic<uint32_t> m_strong{ 1 };
Expand Down Expand Up @@ -743,6 +743,7 @@ namespace winrt::impl
struct __declspec(novtable) root_implements
: root_implements_composing_outer<std::disjunction_v<std::is_same<composing, I>...>>
, root_implements_composable_inner<D, std::disjunction_v<std::is_same<composable, I>...>>
, module_lock_updater<!std::disjunction_v<std::is_same<no_module_lock, I>...>>
{
using IInspectable = Windows::Foundation::IInspectable;
using root_implements_type = root_implements;
Expand Down Expand Up @@ -819,21 +820,12 @@ namespace winrt::impl

root_implements() noexcept
{
if constexpr (use_module_lock::value)
{
++get_module_lock();
}
}

virtual ~root_implements() noexcept
{
// If a weak reference is created during destruction, this ensures that it is also destroyed.
subtract_reference();

if constexpr (use_module_lock::value)
{
--get_module_lock();
}
}

int32_t __stdcall GetIids(uint32_t* count, guid** array) noexcept
Expand Down Expand Up @@ -1060,7 +1052,7 @@ namespace winrt::impl
using is_inspectable = std::disjunction<std::is_base_of<Windows::Foundation::IInspectable, I>...>;
using is_weak_ref_source = std::conjunction<is_inspectable, std::negation<is_factory>, std::negation<std::disjunction<std::is_same<no_weak_ref, I>...>>>;
using use_module_lock = std::negation<std::disjunction<std::is_same<no_module_lock, I>...>>;
using weak_ref_t = impl::weak_ref<is_agile::value>;
using weak_ref_t = impl::weak_ref<is_agile::value, use_module_lock::value>;

std::atomic<std::conditional_t<is_weak_ref_source::value, uintptr_t, uint32_t>> m_references{ 1 };

Expand Down
40 changes: 40 additions & 0 deletions test/old_tests/UnitTests/weak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ namespace
return L"NoWeakRef";
}
};

struct WeakNoModuleLock : implements<WeakNoModuleLock, IStringable, no_module_lock>
{
hstring ToString()
{
return L"WeakNoModuleLock";
}
};
}

TEST_CASE("weak,source")
Expand Down Expand Up @@ -268,3 +276,35 @@ TEST_CASE("weak,lifetime")
REQUIRE(!ref.try_as<IAgileObject>());
}
}

TEST_CASE("weak,module_lock")
{
uint32_t object_count = get_module_lock();

IStringable a = make<Weak>();

// Strong reference counts as an object.
REQUIRE(get_module_lock() == object_count + 1);

weak_ref<IStringable> w = a;
a = nullptr;

// Weak reference should still count as an object.
REQUIRE(get_module_lock() == object_count + 1);
}

TEST_CASE("weak,no_module_lock")
{
uint32_t object_count = get_module_lock();

IStringable a = make<WeakNoModuleLock>();

REQUIRE(get_module_lock() == object_count);

weak_ref<IStringable> w = a;
a = nullptr;

// Weak reference to no-module-lock object is also no-module-lock.
REQUIRE(get_module_lock() == object_count);
}