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

[GitHub] cxfeng1 closed pull request #1625: [iOS] fix rax object leak.

cxfeng1 closed pull request #1625: [iOS] fix rax object leak.
URL: https://github.com/apache/incubator-weex/pull/1625
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m b/ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m
index bc25837c62..c8b1ecafbc 100644
--- a/ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m
+++ b/ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m
@@ -475,65 +475,7 @@ - (void)createInstance:(NSString *)instanceIdString
                 }
                 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 @@ - (void)createInstance:(NSString *)instanceIdString
                 
                 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 702a43826f..1fa68335b7 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 b7b73a58b6..097e3798dc 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 @@ + (long) getUnixFixTimeMillis
     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
 
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services