目录
Gasan Akniev

Add comprehensive tests for Swift ABI traversal, mixed-type cycles, closures, and struct fields

Summary: Add 52 new tests across 3 test files exercising the Swift ABI-based retain cycle detection, covering strong/weak/unowned field classification, closure capture traversal, mixed ObjC/Swift boundary cycles, Swift collection properties, inheritance chains, struct field walking, and additional ObjC coverage.

Swift ABI traversal — reference classification (8 tests) Verify that FBGetObjectStrongReferences with ABI traversal correctly returns only strong references and filters weak/unowned fields:

  • Weak-only, unowned-only, mixed, strong+weak, multiple strong, single strong
  • Cycle with weak back-reference (no cycle) vs strong back-reference (cycle)

Closure capture traversal (6 tests) Verify that closures stored as fields are traversed for captures:

  • Strong capture of self → cycle
  • Weak capture of self → no cycle
  • Unowned capture → no cycle
  • Strong capture of unrelated object (no back-ref) → no cycle
  • Cycle via intermediate object through closure
  • Nil closure → no references

Mixed-type cycles with closures (4 tests) Cross-boundary cycles spanning pure Swift, ObjC-backed Swift, and ObjC:

  • Pure Swift closure capturing ObjC object → cycle
  • Pure Swift closure capturing ObjC-backed Swift → cycle
  • Native ObjC block (via setBlockCapturing:) capturing ObjC-backed Swift → cycle
  • ObjC-backed Swift with closure capturing self → cycle

Multi-capture closures (3 tests)

  • Two strong captures with no back-reference → no cycle
  • Mixed strong + weak captures, target does not point back → no cycle
  • Mixed strong + weak captures, target points back → cycle

Cross-boundary closure chains (2 tests)

  • 4-step chain: ObjC → pure Swift → closure → ObjC-backed Swift → ObjC
  • 3-way: pure Swift → closure → ObjC-backed → pure Swift2 → pure Swift

Edge cases (4 tests)

  • Self-reference, ObjC-backed subclass with pure Swift field, NSObject stored in pure Swift Any? field, protocol-typed property

Lazy var (1 test)

  • Lazy var holding object that points back → cycle

Swift collections (2 tests)

  • Swift Array and Dictionary in pure Swift classes → cycle detected

Deep object chain (1 test)

  • 4-node cycle: A → B → C → D → A

Pure Swift inheritance chain (5 tests)

  • Cycle via superclass field, via child field, both levels have refs, 3-level grandchild all levels, cycle via grandparent field

Struct fields (6 tests)

  • Single ref, mixed value+ref fields, multiple refs, nested struct, weak ref in struct (no cycle), struct + direct ref (both detected)

ObjC detector tests (8 tests, FBRetainCycleDetectorTests.mm)

  • Nested blocks, array/dictionary containing owner, NSHashTable strong, NSMapTable strong, weak back-reference exclusion, deep ObjC inheritance, timer userInfo cycle

Association manager tests (2 tests, FBAssociationManagerTests.mm)

  • Chained associated objects A→B→A cycle
  • Associated block capturing host cycle

New test fixtures:

  • RCDObjectWrapperWithBlock — ObjC class with block + setBlockCapturing: helper that creates native ObjC blocks with standard inline captures (avoids Swift closure-to-block bridging issue)
  • ObjcBackedWithClosure, PureSwiftSubclassOfObjcBacked, PureSwiftWithProtocolRef/PureSwiftProtocolImpl, PureSwiftWithLazyVar, PureSwiftDeepA/B/C/D, PureSwiftBase/Child/Grandchild, PureSwiftWithSwiftArray/Dict, struct types + wrapper classes

Also removes the “Swift struct fields containing references” TODO entry.

Reviewed By: thegreatwallfb

Differential Revision: D98683166

fbshipit-source-id: da3bb66ede90cb3ca0eeba00b097951b764a4cfc

9天前144次提交

FBRetainCycleDetector

Build Status Carthage compatible CocoaPods License

An iOS library that finds retain cycles using runtime analysis.

About

Retain cycles are one of the most common ways of creating memory leaks. It’s incredibly easy to create a retain cycle, and tends to be hard to spot it. The goal of FBRetainCycleDetector is to help find retain cycles at runtime. The features of this project were influenced by Circle.

Installation

Carthage

To your Cartfile add:

github "facebook/FBRetainCycleDetector"

FBRetainCycleDetector is built out from non-debug builds, so when you want to test it, use

carthage update --configuration Debug

CocoaPods

To your podspec add:

pod 'FBRetainCycleDetector'

You’ll be able to use FBRetainCycleDetector fully only in Debug builds. This is controlled by compilation flag that can be provided to the build to make it work in other configurations.

Example usage

Let’s quickly dive in

#import <FBRetainCycleDetector/FBRetainCycleDetector.h>
FBRetainCycleDetector *detector = [FBRetainCycleDetector new];
[detector addCandidate:myObject];
NSSet *retainCycles = [detector findRetainCycles];
NSLog(@"%@", retainCycles);

- (NSSet<NSArray<FBObjectiveCGraphElement *> *> *)findRetainCycles will return a set of arrays of wrapped objects. It’s pretty hard to look at at first, but let’s go through it. Every array in this set will represent one retain cycle. Every element in this array is a wrapper around one object in this retain cycle. Check FBObjectiveCGraphElement.

Example output could look like this:

{(
    (
        "-> MyObject ",
        "-> _someObject -> __NSArrayI "
    )
)}

MyObject through someObject property retained NSArray that it was a part of.

FBRetainCycleDetector will look for cycles that are no longer than 10 objects. We can make it bigger (although it’s going to be slower!).

FBRetainCycleDetector *detector = [FBRetainCycleDetector new];
[detector addCandidate:myObject];
NSSet *retainCycles = [detector findRetainCyclesWithMaxCycleLength:100];

Filters

There could also be retain cycles that we would like to omit. It’s because not every retain cycle is a leak, and we might want to filter them out. To do so we need to specify filters:

NSMutableArray *filters = @[
  FBFilterBlockWithObjectIvarRelation([UIView class], @"_subviewCache"),
];

// Configuration object can describe filters as well as some options
FBObjectGraphConfiguration *configuration =
[[FBObjectGraphConfiguration alloc] initWithFilterBlocks:filters
                                     shouldInspectTimers:YES];
FBRetainCycleDetector *detector = [[FBRetainCycleDetector alloc] initWithConfiguration:configuration];
[detector addCandidate:myObject];
NSSet *retainCycles = [detector findRetainCycles];

Every filter is a block that having two FBObjectiveCGraphElement objects can say, if their relation is valid.

Check FBStandardGraphEdgeFilters to learn more about how to use filters.

NSTimer

NSTimer can be troublesome as it will retain it’s target. Oftentimes it means a retain cycle. FBRetainCycleDetector can detect those, but if you want to skip them, you can specify that in the configuration you are passing to FBRetainCycleDetector.

FBObjectGraphConfiguration *configuration =
[[FBObjectGraphConfiguration alloc] initWithFilterBlocks:someFilters
                                     shouldInspectTimers:NO];
FBRetainCycleDetector *detector = [[FBRetainCycleDetector alloc] initWithConfiguration:configuration];

Associations

Objective-C let’s us set associated objects for every object using objc_setAssociatedObject.

These associated objects can lead to retain cycles if we use retaining policies, like OBJC_ASSOCIATION_RETAIN_NONATOMIC. FBRetainCycleDetector can catch these kinds of cycles, but to do so we need to set it up. Early in the application’s lifetime, preferably in main.m we can add this:

#import <FBRetainCycleDetector/FBAssociationManager.h>

int main(int argc, char * argv[]) {
  @autoreleasepool {
    [FBAssociationManager hook];
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
  }
}

In the code above [FBAssociationManager hook] will use fishhook to interpose functions objc_setAssociatedObject and objc_resetAssociatedObjects to track associations before they are made.

Getting Candidates

If you want to profile your app, you might want to have an abstraction over how to get candidates for FBRetainCycleDetector. While you can simply track it your own, you can also use FBAllocationTracker. It’s a small tool we created that can help you track the objects. It offers simple API that you can query for example for all instances of given class, or all class names currently tracked, etc.

FBAllocationTracker and FBRetainCycleDetector can work nicely together. We have created a small example and drop-in project called FBMemoryProfiler that leverages both these projects. It offers you very basic UI that you can use to track all allocations and force retain cycle detection from UI.

Contributing

See the CONTRIBUTING file for how to help out.

License

FBRetainCycleDetector is BSD-licensed.

    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802032778号