jasenhuang / nsobjectsafe Goto Github PK
View Code? Open in Web Editor NEWSwizzle commonly used function of Foundation container to prevent nil crash
Swizzle commonly used function of Foundation container to prevent nil crash
系统 framework 其实在 objectForKey 传 nil 的时候不会 crash,我这边检测到有些时候弹出键盘系统线程自己就会传 nil 进来
用你这个,ReactiveCocoa就不兼容了,报错
Unknown class name 'NSAttributedStringKey'; did you mean 'NSAttributedString'?
目前发现NSMutableArray的子类发生变化,区分了空内容和非空内容
NSString && NSMutableString not hook ''stringByAppendingString:'' In load Method, 造成崩溃
for iOS9
[[NSArray alloc] init] --> __NSArray0
for iOS8 and lower
[[NSArray alloc] init] --> __NArrayI
so for iOS8 and lower, we don't need to swizzle for two times:
'''
/* 数组有内容obj类型才是__NSArrayI /
NSArray obj = [[NSArray alloc] initWithObjects:@0, nil];
[obj swizzleInstanceMethod:@selector(objectAtIndex:) withMethod:@selector(hookObjectAtIndex:)];
/* 没内容类型是__NSArray0 */
obj = [[NSArray alloc] init];
[obj swizzleInstanceMethod:@selector(objectAtIndex:) withMethod:@selector(hookObjectAtIndex0:)];
'''
有没有具体的使用方法啊??
这是一个能稳定复现的崩溃
从页面A 模态弹出 页面B(页面B中使用self.navigationItem.searchController = self.searchController 添加搜索栏)
此时使用该类库会稳定崩溃,打开僵尸对象,控制台打印*** -[UINavigationBar release]: message sent to deallocated instance 0x7fa305303970
若移除该类库则正常
若无法复现我可以上传demo
为什么不使用ARC?是否可以将release去掉变成ARC?
Cannot create __weak reference in file using manual reference counting
获取具体是哪个控制器哪个方法有问题
例如NSMutableData类别中[hookReplaceBytesInRange:withBytes:],如果有需要替换长度超过原数据长度的情况下,超过长度的数据就被截断,这样的做法容易产生意想不到的后果,希望能再被优化,不是简单地截断后面的数据,其实不截断处理也不会崩溃,不是吗?请尽快想办法优化,已经有开发者遇到此问题了 @jasenhuang @hypoyao @tedzhou
NSObjectSafe/NSObjectSafe/NSObjectSafe.m
Line 868 in 703982e
Setting <= self.length fixed the crash but I don't know whether something else will be broken or not
NSCache进行swizzing交换时,无需判断key为nil的情况,因为setObject:forKey方法里key不遵循NSCoping协议,而且通过本地测试(我用的iOS13)测试,当key为nil时,执行上述set方法不会crash,仍然可以继续执行
Scene:
UIActivityViewController *shareVC = [[UIActivityViewController alloc]initWithActivityItems:@[self.debugText.string] applicationActivities:nil];
[self presentViewController:shareVC animated:YES completion:nil];
Crash:
crash at NSCache(safe) hookSetObject:forKey: ,because input an nil key, but when I don't use NSObjectSafe , it doesn't crash .
1.进接引入头文件就可以了么
2.如果不报错了,bugly还能收集到错误不
需要把array的objectAtIndexedSubscript: 也加一个安全判断
我添加进去之后,直接崩溃了。该设置的地方也都设置过了,不知道该怎么设置
因为多博代理的Selector并没有人注册。。是nil
+ (void)swizzleClassMethod:(SEL)origSelector withMethod:(SEL)newSelector
{
Class cls = [self class];
Method originalMethod = class_getClassMethod(cls, origSelector);
Method swizzledMethod = class_getClassMethod(cls, newSelector);
Class metacls = objc_getMetaClass(NSStringFromClass(cls).UTF8String);
if (class_addMethod(metacls,
origSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod)) ) {
/* swizzing super class method, added if not exist */
class_replaceMethod(metacls,
newSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
- (void)swizzleInstanceMethod:(SEL)origSelector withMethod:(SEL)newSelector
{
Class cls = [self class];
/* if current class not exist selector, then get super*/
Method originalMethod = class_getInstanceMethod(cls, origSelector);
Method swizzledMethod = class_getInstanceMethod(cls, newSelector);
/* add selector if not exist, implement append with method */
if (class_addMethod(cls,
origSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod)) ) {
/* replace class instance method, added if selector not exist */
/* for class cluster , it always add new selector here */
class_replaceMethod(cls,
newSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
/* swizzleMethod maybe belong to super */
class_replaceMethod(cls,
newSelector,
class_replaceMethod(cls,
origSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod)),
method_getTypeEncoding(originalMethod));
}
}
为什么交换实例方法的时候需要
class_replaceMethod(cls,
newSelector,
class_replaceMethod(cls,
origSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod)),
method_getTypeEncoding(originalMethod));
而交换类方法是只需要
method_exchangeImplementations(originalMethod, swizzledMethod);
看了许久,希望能得到帮助和理解
你好,我想在debug模式下生效,怎么使用?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.