Skip to content

Commit 709951e

Browse files
author
Greg Parker
committed
[runtime] Fix a leak when a cast from an existential fails.
rdar://25011739.
1 parent b90a1e9 commit 709951e

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

stdlib/public/runtime/Casting.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,12 +1592,18 @@ static bool _dynamicCastFromExistential(OpaqueValue *dest,
15921592

15931593
bool result = swift_dynamicCast(dest, srcValue, srcCapturedType,
15941594
targetType, subFlags);
1595-
// Deallocate the existential husk if we took from it.
1596-
if (canTake && result && isOutOfLine)
1597-
_maybeDeallocateOpaqueExistential(src, result, flags);
1598-
// If we couldn't take, we still may need to destroy the whole value.
1599-
else if (!canTake && shouldDeallocateSource(result, flags))
1600-
srcType->vw_destroy(src);
1595+
1596+
if (!canTake) {
1597+
// swift_dynamicCast performed no memory management.
1598+
// Destroy the value if requested.
1599+
if (shouldDeallocateSource(result, flags))
1600+
srcType->vw_destroy(src);
1601+
} else {
1602+
// swift_dynamicCast took or destroyed the value as per the original request
1603+
// We may still have an opaque existential container to deallocate.
1604+
if (isOutOfLine)
1605+
_maybeDeallocateOpaqueExistential(src, result, flags);
1606+
}
16011607

16021608
return result;
16031609
}

0 commit comments

Comments
 (0)