Skip to content

Commit

Permalink
iOS : Demo support swift
Browse files Browse the repository at this point in the history
  • Loading branch information
yixiangboy committed May 19, 2020
1 parent eda0775 commit 0f0931d
Show file tree
Hide file tree
Showing 29 changed files with 856 additions and 163 deletions.
2 changes: 1 addition & 1 deletion DoraemonKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Pod::Spec.new do |s|
'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) DoraemonWithMLeaksFinder'
}
ss.dependency 'DoraemonKit/Core'
#ss.dependency 'FBRetainCycleDetector'
ss.dependency 'FBRetainCycleDetector'
end
end

16 changes: 3 additions & 13 deletions iOS/DoraemonKit/Src/Core/Category/UIViewController+Doraemon.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import "UIViewController+Doraemon.h"
#import "UIView+Doraemon.h"
#import "DoraemonHomeWindow.h"
#import "DoraemonUtil.h"

@implementation UIViewController (Doraemon)

Expand Down Expand Up @@ -51,24 +52,13 @@ - (CGRect) fullscreen {
}

+ (UIViewController *)rootViewControllerForKeyWindow{
UIWindow *keyWindow = nil;
if ([[UIApplication sharedApplication].delegate respondsToSelector:@selector(window)]) {
keyWindow = [[UIApplication sharedApplication].delegate window];
}else{
keyWindow = [UIApplication sharedApplication].windows.firstObject;
}

UIWindow *keyWindow = [DoraemonUtil getKeyWindow];
return [keyWindow rootViewController];
}

+ (UIViewController *)topViewControllerForKeyWindow {
UIViewController *resultVC;
UIWindow *keyWindow = nil;
if ([[UIApplication sharedApplication].delegate respondsToSelector:@selector(window)]) {
keyWindow = [[UIApplication sharedApplication].delegate window];
}else{
keyWindow = [UIApplication sharedApplication].windows.firstObject;
}
UIWindow *keyWindow = [DoraemonUtil getKeyWindow];
resultVC = [self _topViewController:[keyWindow rootViewController]];
while (resultVC.presentedViewController) {
resultVC = [self _topViewController:resultVC.presentedViewController];
Expand Down
14 changes: 14 additions & 0 deletions iOS/DoraemonKit/Src/Core/CommonUI/Alert/DoraemonAlertUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ typedef void (^DoraemonAlertCancleActionBlock)(void);
text:(NSString *)text
okBlock:(DoraemonAlertOKActionBlock)okBlock;

+ (void)handleAlertActionWithVC:(UIViewController *)vc
title: (NSString *)title
text:(NSString *)text
ok:(NSString *)ok
okBlock:(DoraemonAlertOKActionBlock)okBlock;

+ (void)handleAlertActionWithVC:(UIViewController *)vc
title: (NSString *)title
text:(NSString *)text
ok:(NSString *)ok
cancel:(NSString *)cancel
okBlock:(DoraemonAlertOKActionBlock)okBlock
cancleBlock:(DoraemonAlertCancleActionBlock)cancleBlock;



@end
Expand Down
34 changes: 34 additions & 0 deletions iOS/DoraemonKit/Src/Core/CommonUI/Alert/DoraemonAlertUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,38 @@ + (void)handleAlertActionWithVC:(UIViewController *)vc
[vc presentViewController:alertController animated:YES completion:nil];
}

+ (void)handleAlertActionWithVC:(UIViewController *)vc
title: (NSString *)title
text:(NSString *)text
ok:(NSString *)ok
okBlock:(DoraemonAlertOKActionBlock)okBlock{
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:text preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:ok style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
okBlock ? okBlock():nil;
}];
[alertController addAction:okAction];
[vc presentViewController:alertController animated:YES completion:nil];
}

+ (void)handleAlertActionWithVC:(UIViewController *)vc
title: (NSString *)title
text:(NSString *)text
ok:(NSString *)ok
cancel:(NSString *)cancel
okBlock:(DoraemonAlertOKActionBlock)okBlock
cancleBlock:(DoraemonAlertCancleActionBlock)cancleBlock {
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:text preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancel style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
cancleBlock ? cancleBlock():nil;
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:ok style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
okBlock ? okBlock():nil;
}];

[alertController addAction:cancelAction];
[alertController addAction:okAction];
[vc presentViewController:alertController animated:YES completion:nil];
}


@end
8 changes: 7 additions & 1 deletion iOS/DoraemonKit/Src/Core/Util/DoraemonUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,13 @@ + (UIWindow *)getKeyWindow{
if ([[UIApplication sharedApplication].delegate respondsToSelector:@selector(window)]) {
keyWindow = [[UIApplication sharedApplication].delegate window];
}else{
keyWindow = [UIApplication sharedApplication].windows.firstObject;
NSArray *windows = [UIApplication sharedApplication].windows;
for (UIWindow *window in windows) {
if (!window.hidden) {
keyWindow = window;
break;
}
}
}
return keyWindow;
}
Expand Down
96 changes: 49 additions & 47 deletions iOS/DoraemonKit/Src/MLeaksFinder/Function/MLeakedObjectProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@

#import "MLeakedObjectProxy.h"
#import "MLeaksFinder.h"
#import "MLeaksMessenger.h"
#import <objc/runtime.h>
#import <UIKit/UIKit.h>
#import "DoraemonMemoryLeakData.h"
#import "DoraemonCacheManager.h"
#import "DoraemonAlertUtil.h"
#import "UIViewController+Doraemon.h"
#import "DoraemonAlertUtil.h"
#import "UIViewController+Doraemon.h"

#if _INTERNAL_MLF_RC_ENABLED
#import <FBRetainCycleDetector/FBRetainCycleDetector.h>
#endif

static NSMutableSet *leakedObjectPtrs;

@interface MLeakedObjectProxy ()<UIAlertViewDelegate>
@interface MLeakedObjectProxy ()
@property (nonatomic, weak) id object;
@property (nonatomic, strong) NSNumber *objectPtr;
@property (nonatomic, strong) NSArray *viewStack;
Expand Down Expand Up @@ -61,13 +64,15 @@ + (void)addLeakedObject:(id)object {

if ([[DoraemonCacheManager sharedInstance] memoryLeakAlert]) {
#if _INTERNAL_MLF_RC_ENABLED
[MLeaksMessenger alertWithTitle:@"Memory Leak"
message:[NSString stringWithFormat:@"%@", proxy.viewStack]
delegate:proxy
additionalButtonTitle:@"Retain Cycle"];
[DoraemonAlertUtil handleAlertActionWithVC:[UIViewController rootViewControllerForKeyWindow] title:@"Memory Leak" text:[NSString stringWithFormat:@"%@", proxy.viewStack] ok:@"OK" cancel:@"Retain Cycle" okBlock:^{

} cancleBlock:^{
[proxy searchRetainCycle];
}];
#else
[MLeaksMessenger alertWithTitle:@"Memory Leak"
message:[NSString stringWithFormat:@"%@", proxy.viewStack]];
[DoraemonAlertUtil handleAlertActionWithVC:[UIViewController rootViewControllerForKeyWindow] title:@"Memory Leak" text:[NSString stringWithFormat:@"%@", proxy.viewStack] ok:@"OK" okBlock:^{

}];
#endif
}
}
Expand All @@ -78,58 +83,55 @@ - (void)dealloc {
dispatch_async(dispatch_get_main_queue(), ^{
[leakedObjectPtrs removeObject:objectPtr];
[[DoraemonMemoryLeakData shareInstance] removeObjectPtr:objectPtr];
[MLeaksMessenger alertWithTitle:@"Object Deallocated"
message:[NSString stringWithFormat:@"%@", viewStack]];
[DoraemonAlertUtil handleAlertActionWithVC:[UIViewController rootViewControllerForKeyWindow] title:@"Object Deallocated" text:[NSString stringWithFormat:@"%@", viewStack] ok:@"OK" okBlock:^{

}];
});
}

#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (!buttonIndex) {
return;
}

- (void)searchRetainCycle{
id object = self.object;
if (!object) {
return;
}

#if _INTERNAL_MLF_RC_ENABLED
dispatch_async(dispatch_get_global_queue(0, 0), ^{
FBRetainCycleDetector *detector = [FBRetainCycleDetector new];
[detector addCandidate:self.object];
NSSet *retainCycles = [detector findRetainCyclesWithMaxCycleLength:20];

BOOL hasFound = NO;
for (NSArray *retainCycle in retainCycles) {
NSInteger index = 0;
for (FBObjectiveCGraphElement *element in retainCycle) {
if (element.object == object) {
NSArray *shiftedRetainCycle = [self shiftArray:retainCycle toIndex:index];
#if _INTERNAL_MLF_RC_ENABLED
dispatch_async(dispatch_get_global_queue(0, 0), ^{
FBRetainCycleDetector *detector = [FBRetainCycleDetector new];
[detector addCandidate:self.object];
NSSet *retainCycles = [detector findRetainCyclesWithMaxCycleLength:20];

BOOL hasFound = NO;
for (NSArray *retainCycle in retainCycles) {
NSInteger index = 0;
for (FBObjectiveCGraphElement *element in retainCycle) {
if (element.object == object) {
NSArray *shiftedRetainCycle = [self shiftArray:retainCycle toIndex:index];

dispatch_async(dispatch_get_main_queue(), ^{
[DoraemonAlertUtil handleAlertActionWithVC:[UIViewController rootViewControllerForKeyWindow] title:@"Retain Cycle" text:[NSString stringWithFormat:@"%@", shiftedRetainCycle] ok:@"OK" okBlock:^{

}];
});
hasFound = YES;
break;
}

dispatch_async(dispatch_get_main_queue(), ^{
[MLeaksMessenger alertWithTitle:@"Retain Cycle"
message:[NSString stringWithFormat:@"%@", shiftedRetainCycle]];
});
hasFound = YES;
++index;
}
if (hasFound) {
break;
}

++index;
}
if (hasFound) {
break;
if (!hasFound) {
dispatch_async(dispatch_get_main_queue(), ^{
[DoraemonAlertUtil handleAlertActionWithVC:[UIViewController rootViewControllerForKeyWindow] title:@"Retain Cycle" text:@"Fail to find a retain cycle" ok:@"OK" okBlock:^{

}];
});
}
}
if (!hasFound) {
dispatch_async(dispatch_get_main_queue(), ^{
[MLeaksMessenger alertWithTitle:@"Retain Cycle"
message:@"Fail to find a retain cycle"];
});
}
});
#endif
});
#endif
}

- (NSArray *)shiftArray:(NSArray *)array toIndex:(NSInteger)index {
Expand Down
2 changes: 1 addition & 1 deletion iOS/DoraemonKit/Src/MLeaksFinder/Function/MLeaksFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#define _INTERNAL_MLF_ENABLED DEBUG
#endif

//#define MEMORY_LEAKS_FINDER_RETAIN_CYCLE_ENABLED 1
#define MEMORY_LEAKS_FINDER_RETAIN_CYCLE_ENABLED 0

#if MEMORY_LEAKS_FINDER_RETAIN_CYCLE_ENABLED
#define _INTERNAL_MLF_RC_ENABLED MEMORY_LEAKS_FINDER_RETAIN_CYCLE_ENABLED
Expand Down
20 changes: 0 additions & 20 deletions iOS/DoraemonKit/Src/MLeaksFinder/Function/MLeaksMessenger.h

This file was deleted.

35 changes: 0 additions & 35 deletions iOS/DoraemonKit/Src/MLeaksFinder/Function/MLeaksMessenger.m

This file was deleted.

2 changes: 1 addition & 1 deletion iOS/DoraemonKitDemo/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ EXTERNAL SOURCES:

CHECKOUT OPTIONS:
FBRetainCycleDetector:
:commit: 66fd5f4b4ba30ab1fae2cb243963556e5228d583
:commit: 1ff2adee84a6ee94a1ae82526104a188774eb90a
:git: https://github.com/facebook/FBRetainCycleDetector.git
YYDebugDatabase:
:commit: b68105eb0784aede05036616c0b94d26b8d97ad0
Expand Down
Loading

0 comments on commit 0f0931d

Please sign in to comment.