music4kid / PLeakSniffer

Detect memory leaks for your iOS project automatically.
http://mrpeak.cn
MIT License
515 stars 63 forks source link

会自动调用某些方法 #4

Open EggmanQi opened 8 years ago

EggmanQi commented 8 years ago

背景: 我的项目中所有 VC 都继承于一个 baseVC : UIViewController, baseVC 里面有一个方法叫 loadingHUD。 loadingHUD 是用来显示 MBProgressHUD 的。

现象: 加入 PLeakSniffer 并使用后,发现所有它会自动调用 loadingHUD。 断点得出的路径是:[vc viewDidAppear:] -> [baseVC viewDidAppear:] -> [UIViewController (PLeak) swizzled_viewDidAppear:] -> [NSObject(PLeakTrack) watchAllRetainedProperties:] -> [baseVC loadingHUD]

出需要注意的是,viewDidAppear 并没有处理任何事务。(only [super viewDidAppear:animated];)

莫名其妙。

wujichao commented 7 years ago

将https://github.com/music4kid/PLeakSniffer/blob/master/PLeakSniffer/Classes/NSObject%2BPLeakTrack.m#L56 这里改成下面的代码可以去掉调用getter引来的副作用

     NSString *s = [NSString stringWithFormat:@"_%@", name];
     // 不触发getter
     Ivar nameIVar = class_getInstanceVariable(self.class, [s UTF8String]);
     if (nameIVar) {
         id cur = object_getIvar(self, nameIVar);
     }
wjiuxing commented 1 year ago

将https://github.com/music4kid/PLeakSniffer/blob/master/PLeakSniffer/Classes/NSObject%2BPLeakTrack.m#L56 这里改成下面的代码可以去掉调用getter引来的副作用

     NSString *s = [NSString stringWithFormat:@"_%@", name];
     // 不触发getter
     Ivar nameIVar = class_getInstanceVariable(self.class, [s UTF8String]);
     if (nameIVar) {
         id cur = object_getIvar(self, nameIVar);
     }

发生崩溃在 id cur = object_getIvar(self, nameIVar);