Skip to content
This repository has been archived by the owner on Jun 1, 2021. It is now read-only.

Why can't detect the memory leak in my case ? #71

Open
luowei opened this issue Jan 13, 2021 · 0 comments
Open

Why can't detect the memory leak in my case ? #71

luowei opened this issue Jan 13, 2021 · 0 comments

Comments

@luowei
Copy link

luowei commented Jan 13, 2021

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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant