Skip to content

Commit 76eac3f

Browse files
committed
修复iOS / OSX平台下调用方法导致内存泄漏问题。
Former-commit-id: 4a22ff77700e2898f6d7622e1eaebcec33354ab7
1 parent ceadf9b commit 76eac3f

File tree

4 files changed

+66
-28
lines changed

4 files changed

+66
-28
lines changed

Source/iOS_OSX/Code/LSCExportMethodDescriptor.h

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,38 @@
1414
@interface LSCExportMethodDescriptor : NSObject
1515

1616
/**
17-
方法签名,如:@@、@I@B
17+
选择子
1818
*/
19-
@property (nonatomic, copy) NSString *methodSignature;
19+
@property (nonatomic, readonly) SEL selector;
2020

2121
/**
22-
调用器
22+
参数签名,如:@@、@I@B
2323
*/
24-
@property (nonatomic, strong) NSInvocation *invocation;
24+
@property (nonatomic, copy, readonly) NSString *paramsSignature;
25+
26+
27+
/**
28+
方法签名
29+
*/
30+
@property (nonatomic, strong, readonly) NSMethodSignature *methodSignature;
31+
32+
33+
/**
34+
初始化
35+
36+
@param selector 选择子
37+
@param methodSignature 方法签名
38+
@param paramsSignature 参数签名
39+
@return 方法描述
40+
*/
41+
- (instancetype)initWithSelector:(SEL)selector
42+
methodSignature:(NSMethodSignature *)methodSignature
43+
paramsSignature:(NSString *)paramsSignature;
44+
45+
46+
/**
47+
创建方法调用器
48+
*/
49+
- (NSInvocation *)createInvocation;
2550

2651
@end

Source/iOS_OSX/Code/LSCExportMethodDescriptor.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,25 @@
1010

1111
@implementation LSCExportMethodDescriptor
1212

13+
- (instancetype)initWithSelector:(SEL)selector
14+
methodSignature:(NSMethodSignature *)methodSignature
15+
paramsSignature:(NSString *)paramsSignature
16+
{
17+
if (self = [super init])
18+
{
19+
_selector = selector;
20+
_methodSignature = methodSignature;
21+
_paramsSignature = paramsSignature;
22+
}
23+
return self;
24+
}
25+
26+
- (NSInvocation *)createInvocation
27+
{
28+
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:self.methodSignature];
29+
invocation.selector = self.selector;
30+
31+
return invocation;
32+
}
33+
1334
@end

Source/iOS_OSX/Code/LSCExportTypeDescriptor.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ - (LSCExportMethodDescriptor *)_methodWithName:(NSString *)name
153153
NSRegularExpression *regExp = [[NSRegularExpression alloc] initWithPattern:signStrRegexp options:0 error:nil];
154154
[methods enumerateObjectsUsingBlock:^(LSCExportMethodDescriptor * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
155155

156-
NSTextCheckingResult *result = [regExp firstMatchInString:obj.methodSignature options:0 range:NSMakeRange(0, obj.methodSignature.length)];
156+
NSTextCheckingResult *result = [regExp firstMatchInString:obj.paramsSignature options:0 range:NSMakeRange(0, obj.paramsSignature.length)];
157157
if (result)
158158
{
159159
[matchMethods addObject:obj];
@@ -170,11 +170,11 @@ - (LSCExportMethodDescriptor *)_methodWithName:(NSString *)name
170170

171171
BOOL hasMatch = YES;
172172
BOOL hasAlternate = NO;
173-
for (int i = 0; i < methodDesc.methodSignature.length; i++)
173+
for (int i = 0; i < methodDesc.paramsSignature.length; i++)
174174
{
175175
if (i < signArr.count)
176176
{
177-
NSString *nativeSign = [methodDesc.methodSignature substringWithRange:NSMakeRange(i, 1)];
177+
NSString *nativeSign = [methodDesc.paramsSignature substringWithRange:NSMakeRange(i, 1)];
178178
NSString *luaSign = signArr[i];
179179

180180
if ([luaSign isEqualToString:@"N"]
@@ -257,7 +257,7 @@ - (LSCExportMethodDescriptor *)_methodWithName:(NSString *)name
257257
//不带参数
258258
[methods enumerateObjectsUsingBlock:^(LSCExportMethodDescriptor * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
259259

260-
if ([obj.methodSignature isEqualToString:@""])
260+
if ([obj.paramsSignature isEqualToString:@""])
261261
{
262262
targetMethod = obj;
263263
*stop = NO;

Source/iOS_OSX/Code/LSCExportsTypeManager.m

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ - (void)_exportsClassMethods:(LSCExportTypeDescriptor *)typeDescriptor
423423
hasExists = NO;
424424
[methodList enumerateObjectsUsingBlock:^(LSCExportMethodDescriptor * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
425425

426-
if ([obj.methodSignature isEqualToString:signStr])
426+
if ([obj.paramsSignature isEqualToString:signStr])
427427
{
428428
hasExists = YES;
429429
*stop = YES;
@@ -433,12 +433,8 @@ - (void)_exportsClassMethods:(LSCExportTypeDescriptor *)typeDescriptor
433433

434434
if (!hasExists)
435435
{
436-
LSCExportMethodDescriptor *methodDesc = [[LSCExportMethodDescriptor alloc] init];
437-
methodDesc.methodSignature = signStr;
438-
439436
NSMethodSignature *sign = [targetTypeDescriptor.nativeType methodSignatureForSelector:selector];
440-
methodDesc.invocation = [NSInvocation invocationWithMethodSignature:sign];
441-
[methodDesc.invocation setSelector:selector];
437+
LSCExportMethodDescriptor *methodDesc = [[LSCExportMethodDescriptor alloc] initWithSelector:selector methodSignature:sign paramsSignature:signStr];
442438

443439
[methodList addObject:methodDesc];
444440
}
@@ -673,7 +669,7 @@ - (void)_exportsInstanceMethods:(LSCExportTypeDescriptor *)typeDescriptor
673669
hasExists = NO;
674670
[methodList enumerateObjectsUsingBlock:^(LSCExportMethodDescriptor * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
675671

676-
if ([obj.methodSignature isEqualToString:signStr])
672+
if ([obj.paramsSignature isEqualToString:signStr])
677673
{
678674
hasExists = YES;
679675
*stop = YES;
@@ -683,13 +679,9 @@ - (void)_exportsInstanceMethods:(LSCExportTypeDescriptor *)typeDescriptor
683679

684680
if (!hasExists)
685681
{
686-
LSCExportMethodDescriptor *methodDesc = [[LSCExportMethodDescriptor alloc] init];
687-
methodDesc.methodSignature = signStr;
688-
689682
NSMethodSignature *sign = [typeDescriptor.nativeType instanceMethodSignatureForSelector:selector];
690-
methodDesc.invocation = [NSInvocation invocationWithMethodSignature:sign];
691-
[methodDesc.invocation setSelector:selector];
692-
683+
LSCExportMethodDescriptor *methodDesc = [[LSCExportMethodDescriptor alloc] initWithSelector:selector methodSignature:sign paramsSignature:signStr];
684+
693685
[methodList addObject:methodDesc];
694686
}
695687
}
@@ -949,7 +941,7 @@ - (NSInvocation *)_invocationWithMethodName:(NSString *)methodName
949941
methodDesc = [typeDesc instanceMethodWithName:methodName arguments:arguments];
950942
}
951943

952-
return methodDesc.invocation;
944+
return [methodDesc createInvocation];
953945
}
954946

955947

@@ -1175,23 +1167,23 @@ static int instanceMethodRouteHandler(lua_State *state)
11751167
index = [LSCEngineAdapter upvalueIndex:2];
11761168
ptr = [LSCEngineAdapter toPointer:state index:index];
11771169
LSCExportTypeDescriptor *typeDescriptor = (__bridge LSCExportTypeDescriptor *)ptr;
1178-
1170+
11791171
index = [LSCEngineAdapter upvalueIndex:3];
11801172
const char *methodNameCStr = [LSCEngineAdapter toString:state index:index];
11811173
NSString *methodName = [NSString stringWithUTF8String:methodNameCStr];
1182-
1174+
11831175
if ([LSCEngineAdapter type:state index:1] != LUA_TUSERDATA)
11841176
{
11851177
NSString *errMsg = [NSString stringWithFormat:@"call %@ method error : missing self parameter, please call by instance:methodName(param)", methodName];
11861178
[LSCEngineAdapter error:state message:errMsg.UTF8String];
11871179
return retCount;
11881180
}
1189-
1181+
11901182
//创建调用会话
11911183
LSCSession *callSession = [exporter.context makeSessionWithState:state];
11921184
NSArray *arguments = [callSession parseArguments];
11931185
id instance = [arguments[0] toObject];
1194-
1186+
11951187
NSInvocation *invocation = [exporter _invocationWithMethodName:methodName
11961188
arguments:arguments
11971189
typeDesc:typeDescriptor
@@ -1203,7 +1195,7 @@ static int instanceMethodRouteHandler(lua_State *state)
12031195
LSCValue *retValue = [typeDescriptor _invokeMethodWithInstance:instance
12041196
invocation:invocation
12051197
arguments:arguments];
1206-
1198+
12071199
if (retValue)
12081200
{
12091201
retCount = [callSession setReturnValue:retValue];
@@ -1214,7 +1206,7 @@ static int instanceMethodRouteHandler(lua_State *state)
12141206
NSString *errMsg = [NSString stringWithFormat:@"call `%@` method fail : argument type mismatch", methodName];
12151207
[LSCEngineAdapter error:state message:errMsg.UTF8String];
12161208
}
1217-
1209+
12181210
[exporter.context destroySession:callSession];
12191211

12201212
return retCount;

0 commit comments

Comments
 (0)