Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/memoryleak #3961

Merged
merged 7 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.os.Handler;
import android.os.Message;

import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand All @@ -34,7 +35,6 @@
import com.tencent.mtt.hippy.modules.javascriptmodules.HippyJavaScriptModuleInvocationHandler;
import com.tencent.mtt.hippy.modules.nativemodules.HippyNativeModuleBase;
import com.tencent.mtt.hippy.modules.nativemodules.HippyNativeModuleInfo;
import com.tencent.mtt.hippy.runtime.builtins.JSValue;
import com.tencent.mtt.hippy.runtime.builtins.array.JSDenseArray;
import com.tencent.mtt.hippy.serialization.PrimitiveValueDeserializer;
import com.tencent.mtt.hippy.serialization.compatible.Deserializer;
Expand All @@ -46,6 +46,7 @@
import com.tencent.mtt.hippy.utils.LogUtils;

import com.tencent.mtt.hippy.utils.UIThreadUtils;
import java.lang.ref.WeakReference;
import java.lang.reflect.Proxy;
import java.nio.ByteBuffer;
import java.util.HashMap;
Expand Down Expand Up @@ -437,14 +438,20 @@ private void onCallNativeFinished(@Nullable HippyCallNativeParams params) {
adapter.onCallNativeFinished(mContext.getComponentName(), params);
}

private void removeRootView(@NonNull final JSDenseArray roots) {
UIThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
if (roots.size() > 0) {
private void removeRootView(@Nullable final JSDenseArray roots) {
final WeakReference<HippyEngineContext> engineContextRef = new WeakReference<>(mContext);
UIThreadUtils.runOnUiThread(() -> {
HippyEngineContext engineContext = engineContextRef.get();
if (engineContext != null) {
if (roots != null && roots.size() > 0) {
Object valueObj = roots.get(0);
if (valueObj instanceof Integer) {
((HippyInstanceLifecycleEventListener) mContext).onInstanceDestroy((Integer) valueObj);
((HippyInstanceLifecycleEventListener) engineContext).onInstanceDestroy((Integer) valueObj);
}
} else {
ViewGroup rootView = engineContext.getRootView();
if (rootView != null) {
((HippyInstanceLifecycleEventListener) engineContext).onInstanceDestroy(rootView.getId());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class RecycleObjectPool extends BasePool<String, RecycleObject> {
private static final String TAG = "RecycleObjectPool";
private final Object mLock = new Object();
private final Map<String, SimplePool<RecycleObject>> mPools = new HashMap<>();
private int mPoolSize = 24;
private int mPoolSize = 12;

public RecycleObjectPool() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ void NativeRenderManager::DestroyRenderDelegate(JNIEnv* j_env) {
j_env->CallVoidMethod(j_object, j_method_id);
JNIEnvironment::ClearJEnvException(j_env);
j_env->DeleteLocalRef(j_class);
persistent_map_.Erase(id_);
}

void NativeRenderManager::InitDensity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import com.tencent.mtt.hippy.modules.Promise;
import com.tencent.mtt.hippy.views.custom.HippyCustomPropsController;
import com.tencent.mtt.hippy.views.hippylist.HippyRecyclerViewController;
import com.tencent.mtt.hippy.views.hippylist.HippyRecyclerViewWrapper;
import com.tencent.mtt.hippy.views.image.HippyImageViewController;
import com.tencent.mtt.hippy.views.list.HippyListItemViewController;
import com.tencent.mtt.hippy.views.modal.HippyModalHostManager;
Expand All @@ -58,6 +57,7 @@
import com.tencent.renderer.Renderer;
import com.tencent.renderer.node.RenderNode;
import com.tencent.renderer.node.VirtualNode;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -74,24 +74,26 @@ public class ControllerManager {
@NonNull
private final Map<Integer, Pool<String, View>> mRecycleViewPools = new HashMap<>();
@Nullable
private Renderer mRenderer;
private final WeakReference<Renderer> mRendererWeakRef;
@Nullable
private static List<Class<?>> sDefaultControllers;

public ControllerManager(@NonNull Renderer renderer) {
mRenderer = renderer;
mRendererWeakRef = new WeakReference<>(renderer);
mControllerRegistry = new ControllerRegistry(renderer);
mControllerUpdateManger = new ControllerUpdateManger<>(renderer);
}

@Nullable
public RenderManager getRenderManager() {
return mRenderer != null ? ((NativeRender) mRenderer).getRenderManager() : null;
Renderer renderer = mRendererWeakRef.get();
return renderer != null ? ((NativeRender) renderer).getRenderManager() : null;
}

@Nullable
public NativeRender getNativeRender() {
return mRenderer != null ? ((NativeRender) mRenderer) : null;
Renderer renderer = mRendererWeakRef.get();
return renderer != null ? ((NativeRender) renderer) : null;
}

@NonNull
Expand Down Expand Up @@ -192,7 +194,6 @@ public void destroy() {
deleteRootView(mControllerRegistry.getRootIdAt(i));
}
}
mRenderer = null;
}

@Nullable
Expand Down Expand Up @@ -234,7 +235,7 @@ public void preCreateView(int rootId, int id, @NonNull String className,
if (view != null || rootView == null || controller == null) {
return;
}
view = controller.createView(rootView, id, mRenderer, className, props);
view = controller.createView(rootView, id, mRendererWeakRef.get(), className, props);
if (view != null) {
addPreView(view, rootId);
}
Expand Down Expand Up @@ -291,7 +292,7 @@ public View createView(@NonNull RenderNode node, PoolType cachePoolType) {
if (controller == null) {
return null;
}
view = controller.createView(rootView, id, mRenderer, className, node.getProps());
view = controller.createView(rootView, id, mRendererWeakRef.get(), className, node.getProps());
node.setNodeFlag(FLAG_UPDATE_LAYOUT);
}
if (view != null) {
Expand Down Expand Up @@ -625,21 +626,23 @@ private String getViewOperationExceptionMessage(int pid, View parent, int id, Vi
}

private void reportRemoveViewException(int pid, View parent, int id, View child) {
if (mRenderer != null) {
Renderer renderer = mRendererWeakRef.get();
if (renderer != null) {
NativeRenderException exception = new NativeRenderException(
REMOVE_CHILD_VIEW_FAILED_ERR,
getViewOperationExceptionMessage(pid, parent, id, child,
"Remove view failed:"));
mRenderer.handleRenderException(exception);
renderer.handleRenderException(exception);
}
}

private void reportAddViewException(int pid, View parent, int id, View child) {
if (mRenderer != null) {
Renderer renderer = mRendererWeakRef.get();
if (renderer != null) {
NativeRenderException exception = new NativeRenderException(ADD_CHILD_VIEW_FAILED_ERR,
getViewOperationExceptionMessage(pid, parent, id, child,
"Add child to parent failed:"));
mRenderer.handleRenderException(exception);
renderer.handleRenderException(exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.tencent.renderer.Renderer;
import com.tencent.renderer.node.RenderNode;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -42,15 +43,13 @@ public class ControllerRegistry {
@NonNull
private final Map<String, ControllerHolder> mControllers = new HashMap<>();
@Nullable
private Renderer mRenderer;
private final WeakReference<Renderer> mRendererWeakRef;

public ControllerRegistry(@NonNull Renderer renderer) {
mRenderer = renderer;
mRendererWeakRef = new WeakReference<>(renderer);
}

public void clear() {
mRenderer = null;
}
public void clear() {}

public void addControllerHolder(String name, ControllerHolder controllerHolder) {
mControllers.put(name, controllerHolder);
Expand All @@ -66,10 +65,11 @@ public ControllerHolder getControllerHolder(String className) {
public HippyViewController getViewController(@NonNull String className) {
ControllerHolder holder = mControllers.get(className);
if (holder == null) {
if (mRenderer != null) {
Renderer renderer = mRendererWeakRef.get();
if (renderer != null) {
NativeRenderException exception = new NativeRenderException(
GET_VIEW_CONTROLLER_FAILED_ERR, "Unknown class name=" + className);
mRenderer.handleRenderException(exception);
renderer.handleRenderException(exception);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.tencent.renderer.utils.PropertyUtils.PropertyMethodHolder;
import com.tencent.renderer.node.RenderNode;

import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -64,7 +65,7 @@ public class ControllerUpdateManger<T> {
NodeProps.OVERFLOW
};
@Nullable
private Renderer mRenderer;
private final WeakReference<Renderer> mRendererWeakRef;
@Nullable
private ComponentController mComponentController;
@Nullable
Expand All @@ -79,11 +80,11 @@ public class ControllerUpdateManger<T> {
}

public ControllerUpdateManger(@NonNull Renderer renderer) {
mRenderer = renderer;
mRendererWeakRef = new WeakReference<>(renderer);
}

public void clear() {
mRenderer = null;

}

public void setCustomPropsController(T controller) {
Expand Down Expand Up @@ -144,10 +145,7 @@ private static boolean isComponentProps(String name) {
return true;
}
// Special case: property "opacity" in TextVirtualNode also need to process in HippyViewController
if (NodeProps.OPACITY.equals(name)) {
return true;
}
return false;
return NodeProps.OPACITY.equals(name);
}

void findViewPropsMethod(Class<?> cls,
Expand Down Expand Up @@ -198,8 +196,9 @@ private void invokePropMethod(@NonNull Object obj, @NonNull Object arg1,
methodHolder.method.invoke(obj, arg1, value);
}
} catch (Exception exception) {
if (mRenderer != null) {
mRenderer.handleRenderException(
Renderer renderer = mRendererWeakRef.get();
if (renderer != null) {
renderer.handleRenderException(
PropertyUtils.makePropertyConvertException(exception, key,
methodHolder.method));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public int getItemViewType(int position) {
return 0;
}
if (node.shouldSticky()) {
return STICK_ITEM_VIEW_TYPE_BASE - node.getItemViewType();
return STICK_ITEM_VIEW_TYPE_BASE - node.getId();
}
return node.getItemViewType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ protected void init() {
public void onDestroy() {
if (stickyHeaderHelper != null) {
stickyHeaderHelper.detachSticky();
stickyHeaderHelper.onDestroy();
}
}

Expand Down
Loading
Loading