Skip to content

Commit

Permalink
解决alloc、new、copy、mutableCopy调用后内存泄漏的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFruity committed Jan 5, 2021
1 parent e151e31 commit fb65865
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 34 deletions.
20 changes: 9 additions & 11 deletions OCRunner/RunnerClasses+Execute.m
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,6 @@ - (nullable MFValue *)execute:(MFScopeChain *)scope {
return [MFValue voidValue];
}
NSUInteger argCount = [sig numberOfArguments];
void *retValuePointer = alloca([sig methodReturnLength]);
//解决多参数调用问题
if (argValues.count + 2 > argCount && sig != nil) {
NSMutableArray *methodArgs = [@[[MFValue valueWithObject:instance],
Expand All @@ -520,6 +519,8 @@ - (nullable MFValue *)execute:(MFScopeChain *)scope {
invoke_functionPointer(msg_send, methodArgs, result, argCount);
return result;
}else{
void *retValuePointer = alloca([sig methodReturnLength]);
char *returnType = (char *)[sig methodReturnType];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
invocation.target = instance;
invocation.selector = sel;
Expand All @@ -531,22 +532,19 @@ - (nullable MFValue *)execute:(MFScopeChain *)scope {
}
// func replaceIMP execute
[invocation invoke];
char *returnType = (char *)[sig methodReturnType];
returnType = removeTypeEncodingPrefix(returnType);
if (*returnType == 'v') {
return [MFValue voidValue];
}
[invocation getReturnValue:retValuePointer];
MFValue *value = [[MFValue alloc] initTypeEncode:returnType pointer:retValuePointer];
// 针对一下方法调用,需要和CF一样,最终都要release. 与JSPatch和Mango中的__bridge_transfer效果相同
if (sel == @selector(alloc) || sel == @selector(new)||
sel == @selector(copy) || sel == @selector(mutableCopy)) {
CFRelease(*(void **)retValuePointer);
}
return value;
}
const char * returnType = [sig methodReturnType];
NSString *selectorName = NSStringFromSelector(sel);
if ([selectorName isEqualToString:@"alloc"] || [selectorName isEqualToString:@"new"] ||
[selectorName isEqualToString:@"copy"] || [selectorName isEqualToString:@"mutableCopy"]) {
return [[MFValue alloc] initTypeEncode:returnType pointer:retValuePointer];
}else{
return [[MFValue alloc] initTypeEncode:returnType pointer:retValuePointer];
}

}
@end
@implementation ORCFuncCall(Execute)
Expand Down
23 changes: 0 additions & 23 deletions OCRunnerDemo/OCRunnerDemo/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,9 @@ @interface AppDelegate ()
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#if DEBUG
NSString *binaryPatchFilePath = [[NSBundle mainBundle] pathForResource:@"binarypatch" ofType:nil];
NSString *jsonPatchFilePath = [[NSBundle mainBundle] pathForResource:@"jsonpatch" ofType:nil];
#else
NSURL *serverFileUrl = [NSURL URLWithString:@"http://127.0.0.1:8086/binarypatch"];
NSString *patchFilePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
patchFilePath = [patchFilePath stringByAppendingPathComponent:@"BinaryPatchFile"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:serverFileUrl];
req.timeoutInterval = 5;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[[[NSURLSession sharedSession] downloadTaskWithRequest:req completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSURL *dest = [NSURL fileURLWithPath:patchFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:dest.path]) {
[[NSFileManager defaultManager] replaceItemAtURL:dest withItemAtURL:location backupItemName:nil options:0 resultingItemURL:nil error:&error];
}else{
[[NSFileManager defaultManager] moveItemAtURL:location toURL:dest error:&error];
}
if (error) {
NSLog(@"%@",error);
}
dispatch_semaphore_signal(semaphore);
}] resume] ;
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
#endif
[ORInterpreter excuteBinaryPatchFile:binaryPatchFilePath];

[ORInterpreter excuteJsonPatchFile:jsonPatchFilePath];

#if __x86_64__ && TARGET_OS_SIMULATOR && !TARGET_OS_IOSMAC
Expand Down

0 comments on commit fb65865

Please sign in to comment.