diff --git a/Source/Layout/ASLayout.mm b/Source/Layout/ASLayout.mm index 7ab43728a..38b7fa378 100644 --- a/Source/Layout/ASLayout.mm +++ b/Source/Layout/ASLayout.mm @@ -172,7 +172,10 @@ - (void)dealloc { if (_retainSublayoutElements.load()) { for (ASLayout *sublayout in _sublayouts) { - CFRelease((__bridge CFTypeRef)sublayout); + // We retained this, so there's no risk of it deallocating on us. + if (let cfElement = (__bridge CFTypeRef)sublayout->_layoutElement) { + CFRelease(cfElement); + } } } } @@ -186,7 +189,8 @@ - (void)retainSublayoutElements } for (ASLayout *sublayout in _sublayouts) { - CFRetain((__bridge CFTypeRef)sublayout->_layoutElement); + // CFBridgingRetain atomically casts and retains. We need the atomicity. + CFBridgingRetain(sublayout->_layoutElement); } }