Skip to content

Commit

Permalink
Rework how weak tracking is performed
Browse files Browse the repository at this point in the history
Due to how the Objective C runtime works, if an object is in the middle of deallocation, a weak store should not be attempted becase the object’s weak table is already freed. Instead, I am now using a proxy objects, which is set as an associated object in question. If the object is in the middle of deallocation, the associated object will release at the end of deallocation.
Closes #428
  • Loading branch information
LeoNatan committed Nov 23, 2017
1 parent 58df5a6 commit c12fe81
Showing 1 changed file with 41 additions and 7 deletions.
48 changes: 41 additions & 7 deletions detox/ios/Detox/GREYIdlingResourcePrettyPrint.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,39 @@
#import "GREYIdlingResourcePrettyPrint.h"
@import ObjectiveC;

@interface __DTXDeallocSafeProxy : NSObject

@property (nonatomic, unsafe_unretained) id object;

@end

@implementation __DTXDeallocSafeProxy

- (void)dealloc
{
self.object = nil;
}

- (instancetype)initWithObject:(id)object
{
self = [super init];
if(self)
{
object = object;
objc_setAssociatedObject(object, "__DTXDeallocSafeProxy", self, OBJC_ASSOCIATION_RETAIN);
}
return self;
}

@end

@interface NSMapTable<KeyType, ObjectType> ()

- (NSArray<ObjectType>*)allValues;

@end

static NSMapTable<GREYAppStateTrackerObject*, id>* __tarckedObjectsMapping;
static NSMapTable<GREYAppStateTrackerObject*, __DTXDeallocSafeProxy*>* __tarckedObjectsMapping;

@interface GREYAppStateTracker (PrettyPrint) @end

Expand All @@ -25,7 +51,8 @@ - (GREYAppStateTrackerObject *)_pp__trackState:(GREYAppState)state forObject:(id
{
GREYAppStateTrackerObject* rv = [self _pp__trackState:state forObject:element];

[__tarckedObjectsMapping setObject:element forKey:rv];
__DTXDeallocSafeProxy* proxy = [[__DTXDeallocSafeProxy alloc] initWithObject:element];
[__tarckedObjectsMapping setObject:proxy forKey:rv];

return rv;
}
Expand Down Expand Up @@ -125,17 +152,24 @@ + (void)load
rv[@"appState"] = stateString;


NSArray* allElements = __tarckedObjectsMapping.allValues;
NSArray<__DTXDeallocSafeProxy*>* allElements = __tarckedObjectsMapping.allValues;

NSMutableArray* elems = [NSMutableArray new];
NSMutableArray* URLs = [NSMutableArray new];

[allElements enumerateObjectsUsingBlock:^(id _Nonnull actualElement, NSUInteger idx, BOOL * _Nonnull stop) {
[elems addObject:[actualElement description]];
[allElements enumerateObjectsUsingBlock:^(__DTXDeallocSafeProxy* _Nonnull actualElement, NSUInteger idx, BOOL * _Nonnull stop) {
id actualObject = actualElement.object;
if(actualObject == nil)
{
NSLog(@"");
return;
}

[elems addObject:[actualObject description]];

if([actualElement isKindOfClass:[NSURLSessionTask class]])
if([actualObject isKindOfClass:[NSURLSessionTask class]])
{
[URLs addObject:[(NSURLSessionTask*)actualElement originalRequest].URL.absoluteString];
[URLs addObject:[(NSURLSessionTask*)actualObject originalRequest].URL.absoluteString];
}
}];

Expand Down

0 comments on commit c12fe81

Please sign in to comment.