日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ios 修复 内存泄露_iOS 内存泄漏如何解决

發(fā)布時間:2024/9/19 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ios 修复 内存泄露_iOS 内存泄漏如何解决 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

5.上面的方法寫了一個 OC 版本的:

.h:

#import

@interface UIViewController (FindLeaks)

// 默認為 NO

@property (nonatomic) BOOL noCheckLeaks;

@end

.m:

//

//? UIViewController+FindLeaks.m

//? Leaks

//

//? Created by sunny on 2017/8/27.

//? Copyright ? 2017年 CepheusSun. All rights reserved.

//

#import "UIViewController+FindLeaks.h"

#import

static const char *noCheckLeaksKey = "noChechLeaksKey";

@interface NSObject (MethodSwizzling)

+ (void)sy_swizzleInstanceSelector:(SEL)origSelector

swizzleSelector:(SEL)swizzleSelector;

@end

@implementation UIViewController (FindLeaks)

#pragma mark - Binding Property

- (BOOL)noCheckLeaks {

return [objc_getAssociatedObject(self, noCheckLeaksKey) boolValue];

}

- (void)setNoCheckLeaks:(BOOL)noCheckLeaks {

objc_setAssociatedObject(self, noCheckLeaksKey, [NSNumber numberWithBool:noCheckLeaks], OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

#pragma mark - Check

+ (void)load {

#if DEBUG

[self sy_swizzleInstanceSelector:@selector(viewDidDisappear:) swizzleSelector:@selector(fl_viewDidDisappear:)];

#endif

}

- (void)fl_viewDidDisappear:(BOOL)animated {

[self fl_viewDidDisappear:animated];

if (!self.noCheckLeaks) {

[self fl_checkDeallocationAfterDelay:2];

}

}

- (void)fl_checkDeallocationAfterDelay:(NSTimeInterval)delay {

UIViewController *root = [self fl_rootParentViewController];

if (self.isMovingFromParentViewController || root.isBeingDismissed) {

NSString *type = NSStringFromClass([self class]);

NSString *disappearanceSource = self.isMovingFromParentViewController ? @"removed from its parent" : @"dismissed";

__weak typeof(self) weakSelf = self;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

NSString *assert = [NSString stringWithFormat:@"%@ not deallocated after being %@",

type, disappearanceSource];

NSAssert(weakSelf == nil,assert);

});

}

}

- (UIViewController *)fl_rootParentViewController {

UIViewController *root = self;

while (root.parentViewController) {

root = root.parentViewController;

}

return root;

}

@end

@implementation NSObject (MethodSwizzling)

+ (void)sy_swizzleInstanceSelector:(SEL)origSelector

swizzleSelector:(SEL)swizzleSelector {

Method origMethod = class_getInstanceMethod(self, origSelector);

Method swizzleMethod = class_getInstanceMethod(self, swizzleSelector);

BOOL isAdd = class_addMethod(self, origSelector, method_getImplementation(swizzleMethod), method_getTypeEncoding(swizzleMethod));

if (!isAdd) {

method_exchangeImplementations(origMethod, swizzleMethod);

}else {

class_replaceMethod(self, swizzleSelector, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));

}

}

@end

只需要在不需要檢查的方法中設(shè)置屬性為 YES 就好了。

總結(jié)

以上是生活随笔為你收集整理的ios 修复 内存泄露_iOS 内存泄漏如何解决的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。