Open luowei opened 3 years ago
I wrote a ViewController to test memory leak detection, but I found that the log did not detect it. Why?
The ViewController is written like this:
@interface TableView : NSObject @property(nonatomic) Byte *space; @property(nonatomic, copy) void (^block)(); -(void)sayHello; @end #define kTenMB 1048576 * 10 @implementation TableView - (instancetype)init { self = [super init]; if (self) { //分配 10MB 内存 self.space = malloc(kTenMB); memset(self.space, 0, kTenMB); } return self; } -(void)sayHello { NSLog(@"Hello!"); } - (void)dealloc { NSLog(@"=== %s",__func__); free(self.space); } @end @interface AViewController () @property(nonatomic, copy) void (^block)(); @property(nonatomic, strong) TableView *tableView; @end @implementation AViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.title = @"A"; self.view.backgroundColor = UIColor.greenColor; // ---- Block ,self leak ---- self.block = ^{ // [self test]; NSLog(@"== title:%@",self.title); }; self.block(); // ---- tableView leak ---- TableView *tableView = [TableView new]; tableView.block = ^{ [tableView sayHello]; }; } -(void)test{ NSLog(@"==== %s %@",__func__,self); } - (void)dealloc { NSLog(@"==== %s %@",__func__,self); } @end
The configuration of FBMemoryProfiler is like this: main.m
#import <UIKit/UIKit.h> #import <FBAllocationTracker/FBAllocationTrackerManager.h> #import <FBRetainCycleDetector/FBRetainCycleDetector.h> #import "AppDelegate.h" int main(int argc, char *argv[]) { [FBAssociationManager hook]; [[FBAllocationTrackerManager sharedManager] startTrackingAllocations]; [[FBAllocationTrackerManager sharedManager] enableGenerations]; NSString *appDelegateClassName; @autoreleasepool { // Setup code that might create autoreleased objects goes here. appDelegateClassName = NSStringFromClass([AppDelegate class]); } return UIApplicationMain(argc, argv, nil, appDelegateClassName); }
AppDelegate.m
#import <FBMemoryProfiler/FBMemoryProfiler.h> #import <FBRetainCycleDetector/FBRetainCycleDetector.h> #import "AppDelegate.h" @interface RetainCycleLoggerPlugin : NSObject <FBMemoryProfilerPluggable> @end @implementation RetainCycleLoggerPlugin - (void)memoryProfilerDidFindRetainCycles:(NSSet *)retainCycles { NSLog(@"=== cycle retains:%@\n", retainCycles); } @end @interface AppDelegate () { FBMemoryProfiler *_memoryProfiler; } @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { _memoryProfiler = [[FBMemoryProfiler alloc] initWithPlugins:@[[RetainCycleLoggerPlugin new]] retainCycleDetectorConfiguration:nil]; [_memoryProfiler enable]; return YES; } @end
I wrote a ViewController to test memory leak detection, but I found that the log did not detect it. Why?
The ViewController is written like this:
The configuration of FBMemoryProfiler is like this: main.m
AppDelegate.m