Latest 0.1.2.1
Homepage https://github.com/GreedBell/FBRetainCycleDetector
License BSD
Platforms ios 6.0, requires ARC
Frameworks Foundation, CoreGraphics, UIKit
Authors ,

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. We also provide an additional patent grant.

Latest podspec

{
    "name": "FBRetainCycleDetector_Bell",
    "version": "0.1.2.1",
    "summary": "Library that helps with detecting retain cycles in iOS apps",
    "homepage": "https://github.com/GreedBell/FBRetainCycleDetector",
    "license": "BSD",
    "authors": {
        "Grzegorz Pstrucha": "[email protected]",
        "Bell": "[email protected]"
    },
    "platforms": {
        "ios": "6.0"
    },
    "source": {
        "git": "https://github.com/GreedBell/FBRetainCycleDetector.git",
        "tag": "0.1.2.1"
    },
    "source_files": [
        "FBRetainCycleDetector",
        "{FBRetainCycleDetector,fishhook}/**/*.{h,m,mm,c}"
    ],
    "requires_arc": [
        "FBRetainCycleDetector/Associations/Internal/FBAssociationManager+Internal.h",
        "FBRetainCycleDetector/Detector/FBNodeEnumerator.h",
        "FBRetainCycleDetector/Detector/FBRetainCycleDetector+Internal.h",
        "FBRetainCycleDetector/Detector/FBRetainCycleDetector.h",
        "FBRetainCycleDetector/FBRetainCycleUtils.h",
        "FBRetainCycleDetector/Filtering/FBStandardGraphEdgeFilters.h",
        "FBRetainCycleDetector/Graph/FBObjectGraphConfiguration.h",
        "FBRetainCycleDetector/Graph/FBObjectiveCBlock.h",
        "FBRetainCycleDetector/Graph/FBObjectiveCGraphElement.h",
        "FBRetainCycleDetector/Graph/FBObjectiveCObject.h",
        "FBRetainCycleDetector/Graph/Internal/FBObjectiveCGraphElement+Internal.h",
        "FBRetainCycleDetector/Graph/Specialization/FBObjectiveCNSCFTimer.h",
        "FBRetainCycleDetector/Layout/Blocks/FBBlockInterface.h",
        "FBRetainCycleDetector/Layout/Classes/FBClassStrongLayout.h",
        "FBRetainCycleDetector/Layout/Classes/Parser/FBParsedStruct.h",
        "FBRetainCycleDetector/Layout/Classes/Parser/FBParsedType.h",
        "FBRetainCycleDetector/Layout/Classes/Parser/FBStructEncodingParser.h",
        "FBRetainCycleDetector/Layout/Classes/Reference/FBIvarReference.h",
        "FBRetainCycleDetector/Layout/Classes/Reference/FBObjectInStructReference.h",
        "FBRetainCycleDetector/Layout/Classes/Reference/FBObjectReference.h",
        "FBRetainCycleDetector/FBRetainCycleUtils.m",
        "FBRetainCycleDetector/Graph/FBObjectGraphConfiguration.m",
        "FBRetainCycleDetector/Graph/FBObjectiveCBlock.m",
        "FBRetainCycleDetector/Graph/FBObjectiveCGraphElement.m",
        "FBRetainCycleDetector/Graph/FBObjectiveCObject.m",
        "FBRetainCycleDetector/Layout/Classes/FBClassStrongLayout.m",
        "FBRetainCycleDetector/Layout/Classes/Parser/FBParsedStruct.m",
        "FBRetainCycleDetector/Layout/Classes/Parser/FBParsedType.m",
        "FBRetainCycleDetector/Layout/Classes/Reference/FBIvarReference.m",
        "FBRetainCycleDetector/Layout/Classes/Reference/FBObjectInStructReference.m",
        "FBRetainCycleDetector/Detector/FBNodeEnumerator.mm",
        "FBRetainCycleDetector/Detector/FBRetainCycleDetector.mm",
        "FBRetainCycleDetector/Filtering/FBStandardGraphEdgeFilters.mm",
        "FBRetainCycleDetector/Graph/Specialization/FBObjectiveCNSCFTimer.mm",
        "FBRetainCycleDetector/Layout/Classes/Parser/FBStructEncodingParser.mm",
        "fishhook/**/*.{c,h}"
    ],
    "public_header_files": [
        "FBRetainCycleDetector/Detector/FBRetainCycleDetector.h",
        "FBRetainCycleDetector/Associations/FBAssociationManager.h",
        "FBRetainCycleDetector/Graph/FBObjectiveCBlock.h",
        "FBRetainCycleDetector/Graph/FBObjectiveCGraphElement.h",
        "FBRetainCycleDetector/Graph/Specialization/FBObjectiveCNSCFTimer.h",
        "FBRetainCycleDetector/Graph/FBObjectiveCObject.h",
        "FBRetainCycleDetector/Graph/FBObjectGraphConfiguration.h",
        "FBRetainCycleDetector/Layout/Classes/Parser/FBParsedStruct.h",
        "FBRetainCycleDetector/Layout/Classes/Parser/FBParsedType.h",
        "FBRetainCycleDetector/Filtering/FBStandardGraphEdgeFilters.h",
        "FBRetainCycleDetector/Layout/Classes/Parser/FBStructEncodingParser.h"
    ],
    "frameworks": [
        "Foundation",
        "CoreGraphics",
        "UIKit"
    ],
    "libraries": "c++"
}

Pin It on Pinterest

Share This