You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by cx...@apache.org on 2018/10/10 03:12:58 UTC

[incubator-weex] branch master updated: [iOS] fix rax object leak. (#1625)

This is an automated email from the ASF dual-hosted git repository.

cxfeng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-weex.git


The following commit(s) were added to refs/heads/master by this push:
     new 4c900cb  [iOS] fix rax object leak. (#1625)
4c900cb is described below

commit 4c900cbe803263b464e5613baf964aac5faba5e1
Author: wqyfavor <wq...@163.com>
AuthorDate: Wed Oct 10 11:12:53 2018 +0800

    [iOS] fix rax object leak. (#1625)
---
 ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m | 63 +--------------------
 ios/sdk/WeexSDK/Sources/Utility/WXUtility.h      |  2 +
 ios/sdk/WeexSDK/Sources/Utility/WXUtility.m      | 72 ++++++++++++++++++++++++
 3 files changed, 77 insertions(+), 60 deletions(-)

diff --git a/ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m b/ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m
index bc25837..c8b1eca 100644
--- a/ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m
+++ b/ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m
@@ -475,65 +475,7 @@ _Pragma("clang diagnostic pop") \
                 }
                 weakSelf.jsBridge.javaScriptContext[@"wxExtFuncInfo"]= nil;
                 
-                NSMutableArray* allKeys = nil;
-                
-                if ([WXUtility useJSCApiForCreateInstance]) {
-                    JSContextRef contextRef = instanceContextEnvironment.context.JSGlobalContextRef;
-                    WXAssert([instanceContextEnvironment isObject], @"Invalid instance context.");
-                    JSValueRef jsException = NULL;
-                    JSObjectRef instanceContextObjectRef = JSValueToObject(contextRef, instanceContextEnvironment.JSValueRef, &jsException);
-                    if (jsException != NULL) {
-                        WXLogError(@"JSValueToObject Exception during create instance.");
-                    }
-                    BOOL somethingWrong = NO;
-                    if (instanceContextObjectRef != NULL) {
-                        JSPropertyNameArrayRef allKeyRefs = JSObjectCopyPropertyNames(contextRef, instanceContextObjectRef);
-                        size_t keyCount = JSPropertyNameArrayGetCount(allKeyRefs);
-                        
-                        allKeys = [[NSMutableArray alloc] initWithCapacity:keyCount];
-                        for (size_t i = 0; i < keyCount; i ++) {
-                            JSStringRef nameRef = JSPropertyNameArrayGetNameAtIndex(allKeyRefs, i);
-                            size_t len = JSStringGetMaximumUTF8CStringSize(nameRef);
-                            if (len > 1024) {
-                                somethingWrong = YES;
-                                break;
-                            }
-                            char* buf = (char*)malloc(len + 5);
-                            if (buf == NULL) {
-                                somethingWrong = YES;
-                                break;
-                            }
-                            bzero(buf, len + 5);
-                            if (JSStringGetUTF8CString(nameRef, buf, len + 5) > 0) {
-                                NSString* keyString = [NSString stringWithUTF8String:buf];
-                                if ([keyString length] == 0) {
-                                    somethingWrong = YES;
-                                    free(buf);
-                                    break;
-                                }
-                                [allKeys addObject:keyString];
-                            }
-                            else {
-                                somethingWrong = YES;
-                                free(buf);
-                                break;
-                            }
-                            free(buf);
-                        }
-                        JSPropertyNameArrayRelease(allKeyRefs);
-                    } else {
-                        somethingWrong = YES;
-                    }
-                    
-                    if (somethingWrong) {
-                        // [instanceContextEnvironment toDictionary] may contain retain-cycle.
-                        allKeys = (NSMutableArray*)[[instanceContextEnvironment toDictionary] allKeys];
-                    }
-                }
-                else {
-                    allKeys = (NSMutableArray*)[[instanceContextEnvironment toDictionary] allKeys];
-                }
-
+                NSArray* allKeys = [WXUtility extractPropertyNamesOfJSValueObject:instanceContextEnvironment];
                 sdkInstance.createInstanceContextResult = [NSString stringWithFormat:@"%@", allKeys];
                 JSGlobalContextRef instanceContextRef = sdkInstance.instanceJavaScriptContext.javaScriptContext.JSGlobalContextRef;
                 JSObjectRef instanceGlobalObject = JSContextGetGlobalObject(instanceContextRef);
@@ -560,7 +502,8 @@ _Pragma("clang diagnostic pop") \
                 
                 if (raxAPIScript) {
                     [sdkInstance.instanceJavaScriptContext executeJavascript:raxAPIScript withSourceURL:[NSURL URLWithString:raxAPIScriptPath]];
-                    sdkInstance.executeRaxApiResult = [NSString stringWithFormat:@"%@", [[sdkInstance.instanceJavaScriptContext.javaScriptContext.globalObject toDictionary] allKeys]];
+                    NSArray* allKeys = [WXUtility extractPropertyNamesOfJSValueObject:sdkInstance.instanceJavaScriptContext.javaScriptContext.globalObject];
+                    sdkInstance.executeRaxApiResult = [NSString stringWithFormat:@"%@", allKeys];
                 }
                 
                 [sdkInstance.apmInstance onStage:KEY_PAGE_STAGES_LOAD_BUNDLE_END];
diff --git a/ios/sdk/WeexSDK/Sources/Utility/WXUtility.h b/ios/sdk/WeexSDK/Sources/Utility/WXUtility.h
index 702a438..1fa6833 100644
--- a/ios/sdk/WeexSDK/Sources/Utility/WXUtility.h
+++ b/ios/sdk/WeexSDK/Sources/Utility/WXUtility.h
@@ -499,4 +499,6 @@ BOOL WXFloatGreaterThanWithPrecision(CGFloat a,CGFloat b,double precision);
 
 + (long) getUnixFixTimeMillis;
 
++ (NSArray<NSString *> *_Nullable)extractPropertyNamesOfJSValueObject:(JSValue *_Nullable)jsvalue;
+
 @end
diff --git a/ios/sdk/WeexSDK/Sources/Utility/WXUtility.m b/ios/sdk/WeexSDK/Sources/Utility/WXUtility.m
index b7b73a5..097e379 100644
--- a/ios/sdk/WeexSDK/Sources/Utility/WXUtility.m
+++ b/ios/sdk/WeexSDK/Sources/Utility/WXUtility.m
@@ -37,6 +37,7 @@
 #import "WXAppMonitorProtocol.h"
 #import "WXConfigCenterProtocol.h"
 #import "WXTextComponent.h"
+#import "WXAssert.h"
 
 #define KEY_PASSWORD  @"com.taobao.Weex.123456"
 #define KEY_USERNAME_PASSWORD  @"com.taobao.Weex.weex123456"
@@ -1009,6 +1010,77 @@ BOOL WXFloatGreaterThanWithPrecision(CGFloat a, CGFloat b ,double precision){
     return sInterval+CACurrentMediaTime()*1000;
 }
 
++ (NSArray<NSString *> *)extractPropertyNamesOfJSValueObject:(JSValue *)jsvalue
+{
+    if (!jsvalue) {
+        return nil;
+    }
+    
+    NSMutableArray* allKeys = nil;
+    
+    if ([self useJSCApiForCreateInstance]) {
+        JSContextRef contextRef = jsvalue.context.JSGlobalContextRef;
+        if (![jsvalue isObject]) {
+            WXAssert(NO, @"Invalid jsvalue for property enumeration.");
+            return nil;
+        }
+        JSValueRef jsException = NULL;
+        JSObjectRef instanceContextObjectRef = JSValueToObject(contextRef, jsvalue.JSValueRef, &jsException);
+        if (jsException != NULL) {
+            WXLogError(@"JSValueToObject Exception during create instance.");
+        }
+        BOOL somethingWrong = NO;
+        if (instanceContextObjectRef != NULL) {
+            JSPropertyNameArrayRef allKeyRefs = JSObjectCopyPropertyNames(contextRef, instanceContextObjectRef);
+            size_t keyCount = JSPropertyNameArrayGetCount(allKeyRefs);
+            
+            allKeys = [[NSMutableArray alloc] initWithCapacity:keyCount];
+            for (size_t i = 0; i < keyCount; i ++) {
+                JSStringRef nameRef = JSPropertyNameArrayGetNameAtIndex(allKeyRefs, i);
+                size_t len = JSStringGetMaximumUTF8CStringSize(nameRef);
+                if (len > 1024) {
+                    somethingWrong = YES;
+                    break;
+                }
+                char* buf = (char*)malloc(len + 5);
+                if (buf == NULL) {
+                    somethingWrong = YES;
+                    break;
+                }
+                bzero(buf, len + 5);
+                if (JSStringGetUTF8CString(nameRef, buf, len + 5) > 0) {
+                    NSString* keyString = [NSString stringWithUTF8String:buf];
+                    if ([keyString length] == 0) {
+                        somethingWrong = YES;
+                        free(buf);
+                        break;
+                    }
+                    [allKeys addObject:keyString];
+                }
+                else {
+                    somethingWrong = YES;
+                    free(buf);
+                    break;
+                }
+                free(buf);
+            }
+            JSPropertyNameArrayRelease(allKeyRefs);
+        } else {
+            somethingWrong = YES;
+        }
+        
+        if (somethingWrong) {
+            // may contain retain-cycle.
+            allKeys = (NSMutableArray*)[[jsvalue toDictionary] allKeys];
+        }
+    }
+    else {
+        allKeys = (NSMutableArray*)[[jsvalue toDictionary] allKeys];
+    }
+    
+    return allKeys;
+}
+
 @end