Skip to content

Commit

Permalink
Use a reference for the cast result to avoid a spurious addref/release
Browse files Browse the repository at this point in the history
  • Loading branch information
dmachaj committed Oct 29, 2024
1 parent dfbac87 commit 6a71ee1
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions cppwinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -1129,9 +1129,13 @@ namespace cppwinrt
if (is_remove_overload(method))
{
// we intentionally ignore errors when unregistering event handlers to be consistent with event_revoker
//
// The `noexcept` versions will crash if check_cast_result throws but that is no different than previous
// behavior where it would not check the cast result and nullptr crash. At least the exception will terminate
// immediately while preserving the error code and local variables.
format = R"( template <typename D%> auto consume_%<D%>::%(%) const noexcept
{%
const auto castedResult = static_cast<% const&>(static_cast<D const&>(*this));
const auto& castedResult = static_cast<% const&>(static_cast<D const&>(*this));
const auto abiType = *(abi_t<%>**)&castedResult;
check_cast_result(abiType);
abiType->%(%);%
Expand All @@ -1142,7 +1146,7 @@ namespace cppwinrt
{
format = R"( template <typename D%> auto consume_%<D%>::%(%) const noexcept
{%
const auto castedResult = static_cast<% const&>(static_cast<D const&>(*this));
const auto& castedResult = static_cast<% const&>(static_cast<D const&>(*this));
const auto abiType = *(abi_t<%>**)&castedResult;
check_cast_result(abiType);
WINRT_VERIFY_(0, abiType->%(%));%
Expand All @@ -1154,7 +1158,7 @@ namespace cppwinrt
{
format = R"( template <typename D%> auto consume_%<D%>::%(%) const
{%
const auto castedResult = static_cast<% const&>(static_cast<D const&>(*this));
const auto& castedResult = static_cast<% const&>(static_cast<D const&>(*this));
const auto abiType = *(abi_t<%>**)&castedResult;
check_cast_result(abiType);
check_hresult(abiType->%(%));%
Expand Down

0 comments on commit 6a71ee1

Please sign in to comment.