Make Description Functions Dealloc-Safe (#2241)

* Make description functions dealloc-safe

* Make -debugDescription also dealloc-safe
This commit is contained in:
Adlai Holler 2016-09-13 17:48:31 -07:00 committed by GitHub
parent 84e633137b
commit 337e908390
3 changed files with 20 additions and 9 deletions

View File

@ -448,6 +448,7 @@ static ASDisplayNodeMethodOverrides GetASDisplayNodeMethodOverrides(Class c)
_layer.delegate = nil; _layer.delegate = nil;
_layer = nil; _layer = nil;
// TODO: Remove this? If supernode isn't already nil, this method isn't dealloc-safe anyway.
[self __setSupernode:nil]; [self __setSupernode:nil];
_pendingViewState = nil; _pendingViewState = nil;
@ -3291,12 +3292,12 @@ static const char *ASDisplayNodeDrawingPriorityKey = "ASDrawingPriority";
[result addObject:@{ @"frame" : [NSValue valueWithCGRect:_view.frame] }]; [result addObject:@{ @"frame" : [NSValue valueWithCGRect:_view.frame] }];
} else if (_layer != nil) { } else if (_layer != nil) {
[result addObject:@{ @"frame" : [NSValue valueWithCGRect:_layer.frame] }]; [result addObject:@{ @"frame" : [NSValue valueWithCGRect:_layer.frame] }];
} else { } else if (_pendingViewState != nil) {
[result addObject:@{ @"frame" : [NSValue valueWithCGRect:self.frame] }]; [result addObject:@{ @"frame" : [NSValue valueWithCGRect:_pendingViewState.frame] }];
} }
// Check supernode so that if we are cell node we don't find self. // Check supernode so that if we are cell node we don't find self.
ASCellNode *cellNode = ASDisplayNodeFindFirstSupernodeOfClass(self.supernode, [ASCellNode class]); ASCellNode *cellNode = ASDisplayNodeFindFirstSupernodeOfClass([self _deallocSafeSupernode], [ASCellNode class]);
if (cellNode != nil) { if (cellNode != nil) {
[result addObject:@{ @"cellNode" : ASObjectDescriptionMakeTiny(cellNode) }]; [result addObject:@{ @"cellNode" : ASObjectDescriptionMakeTiny(cellNode) }];
} }

View File

@ -36,11 +36,21 @@ NS_ASSUME_NONNULL_BEGIN
ASDISPLAYNODE_EXTERN_C_BEGIN ASDISPLAYNODE_EXTERN_C_BEGIN
/// Returns e.g. <MYObject: 0xFFFFFFFF; name = "Object Name"; frame = (0 0; 50 50)> /**
NSString *ASObjectDescriptionMake(id object, NSArray<NSDictionary *> * _Nullable propertyGroups); * Returns e.g. <MYObject: 0xFFFFFFFF; name = "Object Name"; frame = (0 0; 50 50)>
*
* Note: `object` param is autoreleasing so that this function is dealloc-safe.
* No, unsafe_unretained isn't acceptable here the optimizer may deallocate object early.
*/
NSString *ASObjectDescriptionMake(__autoreleasing id object, NSArray<NSDictionary *> * _Nullable propertyGroups);
/// Returns e.g. <MYObject: 0xFFFFFFFF> /**
NSString *ASObjectDescriptionMakeTiny(id object); * Returns e.g. <MYObject: 0xFFFFFFFF>
*
* Note: `object` param is autoreleasing so that this function is dealloc-safe.
* No, unsafe_unretained isn't acceptable here the optimizer may deallocate object early.
*/
NSString *ASObjectDescriptionMakeTiny(__autoreleasing id object);
NSString * _Nullable ASStringWithQuotesIfMultiword(NSString * _Nullable string); NSString * _Nullable ASStringWithQuotesIfMultiword(NSString * _Nullable string);

View File

@ -37,7 +37,7 @@ NSString *ASGetDescriptionValueString(id object) {
return [object description]; return [object description];
} }
NSString *ASObjectDescriptionMake(id object, NSArray<NSDictionary *> *propertyGroups) { NSString *ASObjectDescriptionMake(__autoreleasing id object, NSArray<NSDictionary *> *propertyGroups) {
NSMutableString *str = [NSMutableString stringWithFormat:@"<%@: %p", [object class], object]; NSMutableString *str = [NSMutableString stringWithFormat:@"<%@: %p", [object class], object];
NSMutableArray *components = [NSMutableArray array]; NSMutableArray *components = [NSMutableArray array];
@ -54,7 +54,7 @@ NSString *ASObjectDescriptionMake(id object, NSArray<NSDictionary *> *propertyGr
return str; return str;
} }
NSString *ASObjectDescriptionMakeTiny(id object) { NSString *ASObjectDescriptionMakeTiny(__autoreleasing id object) {
return ASObjectDescriptionMake(object, nil); return ASObjectDescriptionMake(object, nil);
} }