cxfeng1 closed pull request #1544: [Core] Refactor. Fix memory leak and border 
pixel scale.
URL: https://github.com/apache/incubator-weex/pull/1544
 
 
   

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/WXCoreBridge.mm 
b/ios/sdk/WeexSDK/Sources/Bridge/WXCoreBridge.mm
index 40630e47c5..72c07a9a12 100644
--- a/ios/sdk/WeexSDK/Sources/Bridge/WXCoreBridge.mm
+++ b/ios/sdk/WeexSDK/Sources/Bridge/WXCoreBridge.mm
@@ -156,30 +156,40 @@ static id TO_OBJECT(NSString* s)
         return result;
     }
     
-    static void MergeBorderWidthValues(NSMutableDictionary* dict, const 
WXCoreBorderWidth & borders, bool isUpdate)
+    static void MergeBorderWidthValues(NSMutableDictionary* dict,
+                                       const WXCoreBorderWidth & borders,
+                                       bool isUpdate, float pixelScaleFactor)
     {
+        if (pixelScaleFactor <= 0) {
+            pixelScaleFactor = 1.0f;
+        }
         if (borders.getBorderWidth(kBorderWidthTop) != (float)0.0f || 
isUpdate) {
-            dict[@"borderTopWidth"] = 
@(borders.getBorderWidth(kBorderWidthTop));
+            dict[@"borderTopWidth"] = 
@(borders.getBorderWidth(kBorderWidthTop) / pixelScaleFactor);
         }
         if (borders.getBorderWidth(kBorderWidthLeft) != (float)0.0f || 
isUpdate) {
-            dict[@"borderLeftWidth"] = 
@(borders.getBorderWidth(kBorderWidthLeft));
+            dict[@"borderLeftWidth"] = 
@(borders.getBorderWidth(kBorderWidthLeft) / pixelScaleFactor);
         }
         if (borders.getBorderWidth(kBorderWidthBottom) != (float)0.0f || 
isUpdate) {
-            dict[@"borderBottomWidth"] = 
@(borders.getBorderWidth(kBorderWidthBottom));
+            dict[@"borderBottomWidth"] = 
@(borders.getBorderWidth(kBorderWidthBottom) / pixelScaleFactor);
         }
         if (borders.getBorderWidth(kBorderWidthRight) != (float)0.0f || 
isUpdate) {
-            dict[@"borderRightWidth"] = 
@(borders.getBorderWidth(kBorderWidthRight));
+            dict[@"borderRightWidth"] = 
@(borders.getBorderWidth(kBorderWidthRight) / pixelScaleFactor);
         }
     }
     
-    static void MergeBorderWidthValues(NSMutableDictionary* dict, 
std::vector<std::pair<std::string, std::string>>* borders)
+    static void MergeBorderWidthValues(NSMutableDictionary* dict,
+                                       std::vector<std::pair<std::string, 
std::string>>* borders,
+                                       float pixelScaleFactor)
     {
         if (borders == nullptr) {
             return;
         }
+        if (pixelScaleFactor <= 0) {
+            pixelScaleFactor = 1.0f;
+        }
         
         for (auto& p : *borders) {
-            dict[NSSTRING(p.first.c_str())] = NSSTRING(p.second.c_str());
+            dict[NSSTRING(p.first.c_str())] = @(atof(p.second.c_str()) / 
pixelScaleFactor);
         }
     }
     
@@ -367,13 +377,15 @@ static void MergeBorderWidthValues(NSMutableDictionary* 
dict, std::vector<std::p
         NSMutableDictionary* ns_styles = NSDICTIONARY(styles);
         NSDictionary* ns_attributes = NSDICTIONARY(attributes);
         NSArray* ns_events = NSARRAY(events);
-        MergeBorderWidthValues(ns_styles, borders, false);
+        
+        WXSDKInstance* sdkInstance = [WXSDKManager 
instanceForID:ns_instanceId];
+        MergeBorderWidthValues(ns_styles, borders, false, 
sdkInstance.pixelScaleFactor);
         
 #ifdef DEBUG
         WXLogDebug(@"flexLayout -> action: createBody %@ ref:%@", ns_type, 
ns_ref);
 #endif
         
-        WXComponentManager* manager = [WXSDKManager 
instanceForID:ns_instanceId].componentManager;
+        WXComponentManager* manager = sdkInstance.componentManager;
         if (!manager.isValid) {
             return -1;
         }
@@ -412,13 +424,15 @@ static void MergeBorderWidthValues(NSMutableDictionary* 
dict, std::vector<std::p
         NSDictionary* ns_attributes = NSDICTIONARY(attributes);
         NSArray* ns_events = NSARRAY(events);
         NSInteger ns_index = index;
-        MergeBorderWidthValues(ns_styles, borders, false);
+        
+        WXSDKInstance* sdkInstance = [WXSDKManager 
instanceForID:ns_instanceId];
+        MergeBorderWidthValues(ns_styles, borders, false, 
sdkInstance.pixelScaleFactor);
         
 #ifdef DEBUG
         WXLogDebug(@"flexLayout -> action: addElement : %@", ns_componentType);
 #endif
         
-        WXComponentManager* manager = [WXSDKManager 
instanceForID:ns_instanceId].componentManager;
+        WXComponentManager* manager = sdkInstance.componentManager;
         if (!manager.isValid) {
             return -1;
         }
@@ -505,13 +519,15 @@ static void MergeBorderWidthValues(NSMutableDictionary* 
dict, std::vector<std::p
         NSString* ns_instanceId = NSSTRING(pageId);
         NSString* ns_ref = NSSTRING(ref);
         NSMutableDictionary* ns_style = NSDICTIONARY(style);
-        MergeBorderWidthValues(ns_style, border);
+        
+        WXSDKInstance* sdkInstance = [WXSDKManager 
instanceForID:ns_instanceId];
+        MergeBorderWidthValues(ns_style, border, sdkInstance.pixelScaleFactor);
         
 #ifdef DEBUG
         WXLogDebug(@"flexLayout -> action: updateStyles ref:%@, styles:%@", 
ns_ref, ns_style);
 #endif
         
-        WXComponentManager* manager = [WXSDKManager 
instanceForID:ns_instanceId].componentManager;
+        WXComponentManager* manager = sdkInstance.componentManager;
         if (!manager.isValid) {
             return -1;
         }
diff --git a/ios/sdk/WeexSDK/Sources/Layout/WXComponent+Layout.h 
b/ios/sdk/WeexSDK/Sources/Layout/WXComponent+Layout.h
index 57dc21eb7b..a33884769c 100644
--- a/ios/sdk/WeexSDK/Sources/Layout/WXComponent+Layout.h
+++ b/ios/sdk/WeexSDK/Sources/Layout/WXComponent+Layout.h
@@ -64,4 +64,9 @@ extern "C" {
  */
 - (CGFloat)safeContainerStyleWidth;
 
+/**
+ * @abstract Delete css node of a subcomponent.
+ */
+- (void)removeSubcomponentCssNode:(WXComponent *)subcomponent;
+
 @end
diff --git a/ios/sdk/WeexSDK/Sources/Layout/WXComponent+Layout.mm 
b/ios/sdk/WeexSDK/Sources/Layout/WXComponent+Layout.mm
index 41dacfd33b..19d4541736 100644
--- a/ios/sdk/WeexSDK/Sources/Layout/WXComponent+Layout.mm
+++ b/ios/sdk/WeexSDK/Sources/Layout/WXComponent+Layout.mm
@@ -622,4 +622,25 @@ - (void)_resetCSSNode:(NSArray *)styles
     return WeexCore::kJustifyFlexStart;
 }
 
+- (void)removeSubcomponentCssNode:(WXComponent *)subcomponent
+{
+    auto node = subcomponent->_flexCssNode;
+    if (node) {
+        if (_flexCssNode) {
+            _flexCssNode->removeChild(node);
+        }
+        
+        [subcomponent _setRenderObject:nullptr];
+        
+        // unbind subcomponents of subcomponent
+        NSMutableArray* sub_subcomponents = [[NSMutableArray alloc] init];
+        [subcomponent _collectSubcomponents:sub_subcomponents];
+        for (WXComponent* c in sub_subcomponents) {
+            [c _setRenderObject:nullptr];
+        }
+        
+        delete node; // also will delete all children recursively
+    }
+}
+
 @end
diff --git a/ios/sdk/WeexSDK/Sources/Model/WXComponent.mm 
b/ios/sdk/WeexSDK/Sources/Model/WXComponent.mm
index 7883cce317..42bd7b415b 100644
--- a/ios/sdk/WeexSDK/Sources/Model/WXComponent.mm
+++ b/ios/sdk/WeexSDK/Sources/Model/WXComponent.mm
@@ -595,6 +595,7 @@ - (void)_removeSubcomponent:(WXComponent *)subcomponent
 {
     pthread_mutex_lock(&_propertyMutex);
     [_subcomponents removeObject:subcomponent];
+    [self removeSubcomponentCssNode:subcomponent];
     pthread_mutex_unlock(&_propertyMutex);
 }
 


 

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to