Skip to content

Commit

Permalink
修复iOS/OSX下Function提早释放问题
Browse files Browse the repository at this point in the history
Former-commit-id: b84f6c870e78dd302b9ca0dc343e34ebdff5cb5e
  • Loading branch information
vimfung committed May 31, 2017
1 parent f7cd282 commit f7c26af
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
20 changes: 4 additions & 16 deletions Source/iOS_OSX/Code/LSCFunction.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@
#import "LSCContext_Private.h"
#import "LSCValue_Private.h"
#import "LSCTuple_Private.h"
#import "LSCManagedObjectProtocol.h"

@interface LSCFunction () <LSCManagedObjectProtocol>

/**
连接标识
*/
@property (nonatomic, copy) NSString *_linkId;

@end

@implementation LSCFunction

Expand All @@ -29,8 +19,11 @@ - (instancetype)initWithContext:(LSCContext *)context index:(NSInteger)index
if (self = [super init])
{
self.context = context;
self._linkId = [NSString stringWithFormat:@"%p", self];
self.linkId = [NSString stringWithFormat:@"%p", self];

//设置Lua对象到_vars_表中
[self.context.dataExchanger setLubObjectByStackIndex:index objectId:self.linkId];
//进行引用
[self.context retainValue:[LSCValue functionValue:self]];
}

Expand Down Expand Up @@ -112,11 +105,6 @@ - (LSCValue *)invokeWithArguments:(NSArray<LSCValue *> *)arguments

#pragma mark - LSCManagedObjectProtocol

- (NSString *)linkId
{
return self._linkId;
}

- (BOOL)pushWithContext:(LSCContext *)context
{
return YES;
Expand Down
8 changes: 7 additions & 1 deletion Source/iOS_OSX/Code/LSCFunction_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

#import "LSCFunction.h"
#import "lua.h"
#import "LSCManagedObjectProtocol.h"

@class LSCContext;

@interface LSCFunction ()
@interface LSCFunction () <LSCManagedObjectProtocol>

/**
上下文对象
Expand All @@ -24,6 +25,11 @@
*/
@property (nonatomic, copy) NSString *index;

/**
连接标识
*/
@property (nonatomic, copy) NSString *linkId;

/**
初始化Lua方法
Expand Down
10 changes: 10 additions & 0 deletions Source/iOS_OSX/LSCDataExchanger.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@
*/
- (void)getLuaObject:(id)nativeObject;

/**
设置Lua对象
@param index Lua对象在栈中索引
@param objectId 对象标识
*/
- (void)setLubObjectByStackIndex:(NSInteger)index objectId:(NSString *)objectId;

/**
保留对象对应在Lua中的引用
Expand All @@ -60,4 +68,6 @@
*/
- (void)releaseLuaObject:(id)nativeObject;



@end
21 changes: 14 additions & 7 deletions Source/iOS_OSX/LSCDataExchanger.m
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,7 @@ - (LSCValue *)valueByStackIndex:(int)index
if (objectId && (type == LUA_TTABLE || type == LUA_TUSERDATA || type == LUA_TLIGHTUSERDATA || type == LUA_TFUNCTION))
{
//将引用对象放入表中
[self doActionInVarsTable:^{

//放入对象到_vars_表中
lua_pushvalue(state, (int)index);
lua_setfield(state, -2, objectId.UTF8String);

}];
[self setLubObjectByStackIndex:index objectId:objectId];
}

return value;
Expand Down Expand Up @@ -328,6 +322,19 @@ - (void)getLuaObject:(id)nativeObject
}
}

- (void)setLubObjectByStackIndex:(NSInteger)index objectId:(NSString *)objectId
{
lua_State *state = self.context.state;

[self doActionInVarsTable:^{

//放入对象到_vars_表中
lua_pushvalue(state, (int)index);
lua_setfield(state, -2, objectId.UTF8String);

}];
}

- (void)retainLuaObject:(id)nativeObject
{
if (nativeObject)
Expand Down

0 comments on commit f7c26af

Please sign in to comment.